package com.yys.thread; /** * Created by yys on 2017/9/20. * 测试 Thread join方法 * join 方法为合并线程 * 如果不加入join方法便是两个独立线程 (main线程,和MyThread2线程) * join 方法将两个线程合并 此处将MyThread2合并到main线程中 合并后t1.start()执行完成后执行main()方法中for循环 * 输出为 i am abcde i am abcde i am abcde i am abcde i am abcde i am abcde i am abcde i am abcde i am abcde i am abcde i am abcde i am main thread i am main thread i am main thread i am main thread i am main thread i am main thread i am main thread i am main thread i am main thread i am main thread i am main thread * */ public class TestJoin { public static void main(String args[]){ MyThread2 t1 = new MyThread2("abcde"); t1.start(); try { t1.join(); } catch (InterruptedException e) { e.printStackTrace(); } for(int i=0;i<=10;i++){ System.out.println("i am main thread"); } } } class MyThread2 extends Thread{ MyThread2(String name){ super(name); } public void run(){ for(int i=0;i<=10;i++){ System.out.println("i am " + getName()); try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
JAVA多线程join方法入门详解
最新推荐文章于 2023-07-06 13:33:00 发布