
package com.cavaness.quartzbook.chapter3;
public class Test {
public static void main(String[] args) throws InterruptedException {
Thread thread = new TestThread();
thread.start();
int index = 0;
while (true) {
if (index++ == 100) { // 一开始是主线程和子线程交替执行的,主线程执行完100次后,把子线程合并进来,也即是说等待子线程执行完5秒后,再次主线程和子线程交替执行
try {
thread.join(5000);
} catch (InterruptedException e) {
e.printStackTrace();
throw e;
}
}
System.out.println("Method main:" + Thread.currentThread().getName());
}
}
}
package com.cavaness.quartzbook.chapter3;
public class TestThread extends Thread {
public void run() {
while (true) {
System.out.println("Method run:" + Thread.currentThread().getName());
}
}
}