1. 线程插队join使用
public class JoinThread implements Runnable {
public static void main(String[] args) throws InterruptedException {
JoinThread JoinThread = new JoinThread();
Thread thread = new Thread(JoinThread);
thread.start();
for (int i = 0; i < 1000; i++) {
if (i == 200) thread.join(); //线程插队
System.out.println("执行Main线程!" );
}
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("线程插队了!" + Thread.currentThread().getName());
}
}
}
2. 线程的监测State使用
package Pratice;
//观测线程
//利用state来观测线程
public class SeeThread {
public static void main(String[] args) throws InterruptedException {
Thread thread=new Thread(()->{
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("///");
});
//观察线程,枚举类State
Thread.State state=thread.getState();
System.out.println(state);//新建状态NEW
//观察启动后
thread.start();//启动线程Runnable
state=thread.getState();
System.out.println(state); //Run
while(state!=Thread.State.TERMINATED){
//只要线程不终止,就会一直输出状态,terminated终止
Thread.sleep(100);
state=thread.getState();//更新状态
System.out.println(state);
}
}
}
3. 线程的状态