JAVA多线程之合并线程join

一个线程A在占有cpu资源期间,可以让其他资源调用join()和本线程联合,如:
B.join();

当A在运行期间联合了B,A线程立即中断执行,一直等到他联合的B线程执行完毕,A线程再重新排队等待CPU资源。

代码

package 合并线程join;
public class JOIN {
	
	public static void main(String[] args) {
		Thread t =new Thread(()->{ //lambde模式
			for (int i = 1; i <= 5; i++) {
				System.out.println(Thread.currentThread().getName()+i);
			}
		},"小美") ;
				
		t.start();
		for (int i = 1; i <=5; i++) {
			System.out.println(i);
			if(i%3==0) {
				try {
					t.join();//插队
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}//
			}
		}
	}
}

在这里插入图片描述

发布了27 篇原创文章 · 获赞 5 · 访问量 637

猜你喜欢

转载自blog.csdn.net/qq_44620773/article/details/104170954