Thread.join()方法

import java.util.concurrent.TimeUnit;
public class newc {
    public static void main(String []args){

ThreadTest t =new ThreadTest();
Thread tt =new Thread(t);
      tt.start();


int flag=0;


for(int n1=0;n1<10;n1++)
{


    if(flag==0){try{tt.join(10000);}
    catch (Exception e){e.printStackTrace();}
}
 flag++;   System.out.println(Thread.currentThread().getName()+"--->"+n1);}
}}
class ThreadTest  implements Runnable{

    public void run() {

            for (int i = 1; i < 20; i++) {
                System.out.println(Thread.currentThread().getName() + "这是运行的第" + i + "秒");
                try {
                    TimeUnit.SECONDS.sleep(1);

                } catch (InterruptedException e) {

                }
                if (i == 10) {
                   break;
                }
            }

    }

    }

做到怎么在运行10s的多线程后,main方法中断,用.join()方法

猜你喜欢

转载自www.cnblogs.com/otakus/p/12181372.html