runnable分为获得cpu时间正在运行running,没获得cpu时间ready,io阻塞
blocked就是没得到锁
waiting就是调用了wait,join,locksupport.park
time_waiting就是调用了有时限的sleep,wait,join,locksupport.park
你sleep和wait都立刻释放了CPU时间,但是wait释放了锁,sleep没有释放
t1线程使用t2.join()方法,立刻让出cpu时间,但不一定让t2立刻获得cpu时间,如果有t3线程,t2和t3还是要竞争。join()方法的意思是,如果t2没有结束,t1则一直阻塞,知道t2结束
调用join()不会让出锁
public class Test24 {
public static void main(String[] args) {
Thread t1 = new Thread(()->{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("t1获得cpu时间");
});
Thread t2 = new Thread(()->{
System.out.println("t2获得cpu时间,调用t1.join()");
try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Thread t3 = new Thread(()->{
System.out.println("t3获得cpu时间");
});
t1.start();
t2.start();
t3.start();
}
}
结果