免费直播网站开发,做网站什么用,宁波网站运营优化系统,浏阳网站开发公司线程联合
当前线程邀请调用方法的线程优先执行#xff0c;在调用方法的线程执行结束之前#xff0c;当前线程不能再次执行。线程A在运行期间#xff0c;可以调用线程B的join()方法#xff0c;让线程B和线程A联合。这样#xff0c;线程A就必须等待线程B执行完毕后#xf…线程联合
当前线程邀请调用方法的线程优先执行在调用方法的线程执行结束之前当前线程不能再次执行。线程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 i0;i10;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 i0;i20;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 i0;i10;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 i0;i10;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 i0;i4;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());}
}