当前位置 : 主页 > 编程语言 > c++ >

java线程中的join()方法的使用案例

来源:互联网 收集:自由互联 发布时间:2021-07-03
通过join方法阻塞其它线程,等待该线程执行完毕。 package cn.mym.thread;public class TestJoin {public static void main(String[] args) {Thread5 thread = new Thread5();thread.start();int i = 0;while(i10){try {Thread.sleep(50
通过join方法阻塞其它线程,等待该线程执行完毕。
package cn.mym.thread;

public class TestJoin {
	public static void main(String[] args) {
		Thread5 thread = new Thread5();
		thread.start();
		
		int i = 0;
		while(i<10){
			try {
				Thread.sleep(50);
				if(i == 5){
					thread.join();
				}
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			System.out.println("world");
			i++;
		}
	}

	
}

class Thread5 extends Thread{

	@Override
	public void run() {
		int i = 0;
		while(i<10){
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			System.out.println("Thread4");
			i++;
			
		}
		
	}
	
}




//====================执行结果=================
world
Thread4
Thread4
world
Thread4
world
Thread4
world
Thread4
world
Thread4
Thread4
Thread4
Thread4
Thread4
world
world
world
world
world
网友评论