合并子线程

来源:互联网 发布:淘宝拍照用哪款相机好 编辑:程序博客网 时间:2024/05/22 10:44

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());}}}


原创粉丝点击