image.png
new Thread()
thread.start()
启动线程。synchronized
标记的代码,获取到同步监视器。obj.notify()
唤醒线程。obj.notifyAll()
唤醒线程。obj.wait(time), thread.join(time)
等待时间time耗尽。threadB.join()
,threadA将Waiting,直到threadB终止。obj.wait()
释放同步监视器obj,并进入阻塞状态。threadB.join(time)
。obj.wait(time)
sleep(time)
。wait()
/wait(time)
导致线程处于Waiting
/TimedWaiting
状态,当线程被notify()
/notifyAll()
/wait等待时间到之后,如果没有获取到同步监视器。会直接进入Blocked
阻塞状态。image.png
/**
* 线程的状态演示:NEW,Runnable,Terminated
*
* @author futao
* @date 2020/6/7
*/
public class NewRunnableTerminated implements Runnable {
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println(i);
}
}
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new NewRunnableTerminated());
System.out.println("因为还未调用start(),所以预期`New`:" + thread.getState());
thread.start();
//等待子线程跑一会,看看运行中的状态是不是Runnable
Thread.sleep(10L);
System.out.println("线程start()之后,预期Runnable:" + thread.getState());
//等待子线程执行完毕,再查看其状态
Thread.sleep(1000L);
System.out.println("线程任务执行完毕,预期Terminated:" + thread.getState());
}
}
image.png
image.png
image.png
/**
* 演示Blocked,Waiting,TimedWaiting
*
* @author futao
* @date 2020/6/7
*/
public class BlockedWaitingTimedWaiting implements Runnable {
@Override
public void run() {
try {
sync();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 同步方法
*
* @throws InterruptedException
*/
public synchronized void sync() throws InterruptedException {
Thread.sleep(1000L);
wait();
}
public static void main(String[] args) throws InterruptedException {
BlockedWaitingTimedWaiting blockedWaitingTimedWaiting = new BlockedWaitingTimedWaiting();
Thread thread1 = new Thread(blockedWaitingTimedWaiting);
Thread thread2 = new Thread(blockedWaitingTimedWaiting);
thread1.start();
//为了尽可能让thread1先执行,获取到同步方法的锁
Thread.sleep(10L);
thread2.start();
Thread.sleep(500L);
System.out.println("因为thread1处于sleep(time)状态,所以预期线程状态为<TimedWaiting>: " + thread1.getState());
System.out.println("因为sync()方法的锁被thread1持有,所以thread2被阻塞,预期状态为<Blocked>: " + thread2.getState());
//等待thread1的sleep(time)时间结束,进入wait()方法
Thread.sleep(1000L);
System.out.println("因为thread1的sleep(time)时间结束,进入wait()方法,所以预期thread1的线程状态为<Waiting>:" + thread1.getState());
}
}
image.png
欢迎在评论区留下你看文章时的思考,及时说出,有助于加深记忆和理解,还能和像你一样也喜欢这个话题的读者相遇~