当前位置: 首页 > news >正文

JAVA深化篇_29—— 线程使用之线程联合以及Thread类中的其他常用方法【附有详细说明及代码案例】

线程联合

当前线程邀请调用方法的线程优先执行,在调用方法的线程执行结束之前,当前线程不能再次执行。线程A在运行期间,可以调用线程B的join()方法,让线程B和线程A联合。这样,线程A就必须等待线程B执行完毕后,才能继续执行。

join方法的使用

join()方法就是指调用该方法的线程在执行完run()方法后,再执行join方法后面的代码,即将两个线程合并,用于实现同步控制。

class A implements Runnable{private Thread b;public A(Thread b){this.b = b;}@Overridepublic void run() {for(int i=0;i<10;i++){System.out.println(Thread.currentThread().getName()+"  A  "+i);if(i == 5){try {this.b.join();} catch (InterruptedException e) {e.printStackTrace();}}try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}
}class B implements Runnable{@Overridepublic void run() {for(int i=0;i<20;i++){System.out.println(Thread.currentThread().getName()+" B "+i);try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}
}public class TestJoinThread {public static void main(String[] args) {Thread t1 = new Thread(new B());Thread t = new Thread(new A(t1));t.start();t1.start();for(int i=0;i<10;i++){System.out.println(Thread.currentThread().getName()+" "+i);if(i ==2){try {t.join();} catch (InterruptedException e) {e.printStackTrace();}}try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}
}

线程联合案例

需求:

实现爸爸让儿子买烟。

/*** 儿子买烟线程*/
class SonThread implements Runnable{@Overridepublic void run() {System.out.println("儿子出门买烟");System.out.println("儿子买烟需要10分钟");for(int i=0;i<10;i++){System.out.println("第"+i+"分钟");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}System.out.println("儿子买烟回来了");}
}/*** 爸爸抽烟线程*/
class FatherThread implements Runnable{@Overridepublic void run() {System.out.println("爸爸想抽烟,发现烟抽完了");System.out.println("爸爸让儿子去买一包红塔山");Thread t = new Thread(new SonThread());t.start();System.out.println("等待儿子买烟回来");try {t.join();} catch (InterruptedException e) {e.printStackTrace();System.out.println("爸爸出门找儿子");System.exit(1);}System.out.println("爸爸高兴的接过烟,并把零钱给了儿子");}
}public class TestJoinDemo {public static void main(String[] args) {System.out.println("爸爸和儿子买烟的故事");Thread t = new Thread(new FatherThread());t.start();}
}

Thread类中的其他常用方法

获取线程名称getName()
方式一

this.getName()获取线程名称,该方法适用于继承Thread实现多线程方式。

class GetName1 extends Thread{@Overridepublic void run() {System.out.println(this.getName());}
}
方式二

Thread.currentThread().getName()获取线程名称,该方法适用于实现Runnable接口实现多线程方式。

class GetName2 implements Runnable{@Overridepublic void run() {System.out.println(Thread.currentThread().getName());}
}
设置线程的名称setName()
方式一

通过构造方法设置线程名称。

class SetName1 extends Thread{public SetName1(String name){super(name);}@Overridepublic void run() {System.out.println(this.getName());}
}public class SetNameThread {public static void main(String[] args) {SetName1 setName1 = new SetName1("SetName1");setName1.start();}
}
方式二

通过setName()方法设置线程名称。

class SetName2 implements Runnable{@Overridepublic void run() {System.out.println(Thread.currentThread().getName());}
}
public class SetNameThread {public static void main(String[] args) {Thread thread = new Thread(new SetName2());thread.setName("SetName2");thread.start();}
}
判断线程是否存活isAlive()

isAlive()方法: 判断当前的线程是否处于活动状态。

活动状态是指线程已经启动且尚未终止,线程处于正在运行或准备开始运行的状态,就认为线程是存活的。

class Alive implements Runnable{@Overridepublic void run() {for(int i=0;i<4;i++){System.out.println(Thread.currentThread().getName()+" "+i);try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}}}
}public class TestAliveThread {public static void main(String[] args) {Thread thread = new Thread(new Alive());thread.setName("Alive");thread.start();System.out.println(thread.getName()+" "+thread.isAlive());try {Thread.sleep(4000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(thread.getName()+" "+thread.isAlive());}
}
http://www.lryc.cn/news/219097.html

相关文章:

  • 〖Python网络爬虫实战㊲〗- JavaScript 逆向实战(一)
  • 2023辽宁省数学建模A题铁路车站的安全标线完整原创论文详细讲解(含matlab代码)
  • (14)学习笔记:动手深度学习(Pytorch神经网络基础)
  • Leetcode-1 两数之和
  • Screens for Mac 中文版 远程桌面连接控制工具
  • 解决vmware安装ubuntu虚拟机显示不全以及无法实现windows与虚拟机之间无法相互复制粘贴问题
  • 希腊字母读音表
  • 如何使用CodeceptJS、Playwright和GitHub Actions构建端到端测试流水线
  • 解析python爬取Ebay数据的方式
  • 设置DevC++支持c++11标准
  • 腾讯云服务器CVM详细介绍_优缺点亲自整理
  • 06_es分布式搜索引擎2
  • 【3D图像分割】基于 Pytorch 的 VNet 3D 图像分割3(3D UNet 模型篇)
  • 【源码解析】Spring Bean定义常见错误
  • 由于找不到vcruntime140.dll无法继续执行代码
  • Perl安装教程
  • Docker数据卷使用过程中想到的几个问题
  • Angular 中的路由
  • 【市场分析】Temu数据采集销售额商品量占比分析数据分析接口Api
  • Python笔记——linux/ubuntu下安装mamba,安装bob.learn库
  • Redis之Java操作Redis的使用
  • 《网络协议》01. 基本概念
  • 设置Ubuntu网络代理
  • LeetCode----23. 合并 K 个升序链表
  • [极客大挑战 2019]LoveSQL 1
  • dji mini4pro 图片拷贝到电脑速度
  • 基于深度学习的目标检测算法 计算机竞赛
  • 前端面试题之CSS篇
  • 【SQL相关实操记录】
  • Python爬虫实战-批量爬取下载网易云音乐