代码:
package com.test.thread;
public class TestRunnable implements Runnable{
@Override
public 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方法时 其他线程要 等待他执行完 在执行
标签:TestRunnable,Java,Thread,____,runnable,线程,new,多线程,public From: https://blog.51cto.com/ratelcloud/7454306