Java多线程____线程状态Join()方法

来源:互联网 发布:手机设计软件推荐 编辑:程序博客网 时间:2024/06/11 05:09

代码:

package com.test.thread;public class TestRunnable implements Runnable{@Overridepublic void run() {synchronized (this) {for (int i = 0; i < 10; i++) {System.out.println(Thread.currentThread().getName());}}}}

package com.test.thread;public class TestMain {public static void main(String[] args) {TestRunnable runnable_01=new TestRunnable();Thread thread1=new Thread(runnable_01, "runnable_01");TestRunnable runnable_02=new TestRunnable();Thread thread2=new Thread(runnable_02, "runnable_02");thread1.start();try {thread1.join();} catch (InterruptedException e) {e.printStackTrace();}thread2.start();}}

运行结果

Join的JDK官方文档说的等待线程终止

意思就是说某个子线程调用join方法时 其他线程要 等待他执行完 在执行

原创粉丝点击