publicclassJoinThreadimplementsRunnable{publicstaticvoidmain(String[] args)throwsInterruptedException{JoinThreadJoinThread=newJoinThread();Thread thread =newThread(JoinThread);
thread.start();for(int i =0; i <1000; i++){if(i ==200) thread.join();//线程插队System.out.println("执行Main线程!");}}@Overridepublicvoidrun(){for(int i =0; i <100; i++){System.out.println("线程插队了!"+Thread.currentThread().getName());}}}
2. 线程的监测State使用
packagePratice;//观测线程//利用state来观测线程publicclassSeeThread{publicstaticvoidmain(String[] args)throwsInterruptedException{Thread thread=newThread(()->{for(int i =0; i <5; i++){try{Thread.sleep(1000);}catch(InterruptedException e){
e.printStackTrace();}}System.out.println("///");});//观察线程,枚举类StateThread.State state=thread.getState();System.out.println(state);//新建状态NEW//观察启动后
thread.start();//启动线程Runnable
state=thread.getState();System.out.println(state);//Runwhile(state!=Thread.State.TERMINATED){//只要线程不终止,就会一直输出状态,terminated终止Thread.sleep(100);
state=thread.getState();//更新状态System.out.println(state);}}}