Thread state --- Day24

Thread Status Overview:

  When the thread is created and started, it is neither a start to enter the execution state, nor is it has been in a state execution. In the thread of the life cycle, there are several state? Given six thread state in the API java.lang.Thread.State this enumeration

Timed Waiting (timed wait) 

  Timed Waiting described in the API is: a limit is waiting for another thread executes a thread (wake-up) action in this state

  In the case of selling tickets we write, in order to reduce the thread execution is too fast, and so is not obvious, we add in the run method of sleep statement, forcing a thread to sleep (suspended) currently being executed in order to "slow down threads. " In fact, when we call sleep method currently executing thread enters into "sleep state", in fact, the so-called Timed Waiting (timing waiting), then we a deeper understanding of the state by a case.

. 1  Package demosummary.threadstate;
 2  
. 3  / ** 
. 4  * implement a counter to count 100, a pause between each number 1 second, a digital output 10 every string
 . 5   * / 
. 6  public  class the ThreadState the extends the Thread {
 . 7      @Override
 . 8      public  void RUN () {
 . 9          for ( int I =. 1; I <100; I ++ ) {
 10              IF (I% 10 == 0 ) {
 . 11                  System.out.println ( "every 10 digital outputs a string " );
 12 is              }
 13 is              System.out.println (I);
 14             try {
15                 Thread.sleep(1000);
16             } catch (InterruptedException e) {
17                 e.printStackTrace();
18             }
19         }
20     }
21 
22     public static void main(String[] args) {
23         new ThreadState().start();
24     }
25 }

  Can be found through the case, using the sleep method is very simple. We need to keep in mind the following points:
    1. A common scenario is to enter the sleep state TIMED_WAITING method call, you can also call a separate thread, do not have to have a collaborative relationship.

    2. In order to allow other threads have a chance to execute, you can call Thread.sleep () is put () within the thread run. So as to ensure the implementation process of the thread will sleep

    3. sleep regardless of the lock, the thread sleep expire automatically wake up and return to Runnable (run) state.

    tip:sleep()中指定的时间是线程不会运行的最短时间。因此,sleep()方法不能保证该线程睡眠到期后就开始立刻执行

  Timed Waiting线程状态图

     

 

BLOCKED(锁阻塞)

  Blocked状态在API中的介绍为:一个正在阻塞等待一个监视器锁(锁对象)的线程处于这一状态,比如,线程A与线程B代码中使用同一锁,如果线程A获 取到锁,线程A进入到Runnable状态,那么线程B就进入到Blocked锁阻塞状态。

  blocked线程状态图

  

 

Waiting(无限等待) 

  Wating状态在API中介绍为:一个正在无限期等待另一个线程执行一个特别的(唤醒)动作的线程处于这一状态

 1 package demosummary.threadstate;
 2 
 3 public class ThreadWaiting {
 4     public static Object obj = new Object();
 5 
 6     public static void main(String[] args) {
 7         new Thread(new Runnable() {
 8             @Override
 9             public void run() {
10                 while (true) {
11                     synchronized (obj) {
12                         try {
13                             System.out.println(Thread.currentThread().getName()+"获取到锁对象,调用wait方法,进入等待状态");
14                             obj.wait();
15                             //也可以设置等待时间,时间到自动唤醒
16                             //obj.wait(3000);
17                         } catch (InterruptedException e) {
18                             e.printStackTrace();
19                         }
20                         System.out.println(Thread.currentThread().getName()+"---从waiting状态醒来,继续执行");
21                     }
22                 }
23             }
24         },"等待线程").start();
25         new Thread(new Runnable() {
26             @Override
27             public void run() {
28                 while (true) {
29                     try {
30                         System.out.println(Thread.currentThread().getName()+"---等待三秒后自动唤醒");
31                         Thread.sleep(3000);
32                     } catch (InterruptedException e) {
33                         e.printStackTrace();
34                     }
35 
36                     synchronized (obj) {
37                         System.out.println(Thread.currentThread().getName() + "获取到锁对象,调用notify方法,释放对象");
38                         obj.notify();
39                     }
40                 }
41             }
42         },"等待线程").start();
43     }
44 } 

  通过上述案例我们会发现,一个调用了某个对象的 Object.wait 方法的线程会等待另一个线程调用此对象的 Object.notify()方法 或 Object.notifyAll()方法。其实waiting状态并不是一个线程的操作,它体现的是多个线程间的通信,可以理解为多个线程之间的协作关系, 多个线程会争取锁,同时相互之间又存在协作关系。就好比在公司里你和你的同事们,你们可能存在晋升时的竞 争,但更多时候你们更多是一起合作以完成某些任务。

  当多个线程协作时,比如A,B线程,如果A线程在Runnable(可运行)状态中调用了wait()方法那么A线程就进入 了Waiting(无限等待)状态,同时失去了同步锁。假如这个时候B线程获取到了同步锁,在运行状态中调用了 notify()方法,那么就会将无限等待的A线程唤醒。注意是唤醒,如果获取到锁对象,那么A线程唤醒后就进入 Runnable(可运行)状态;如果没有获取锁对象,那么就进入到Blocked(锁阻塞状态)

  

  

  我们在翻阅API的时候会发现Timed Waiting(计时等待) 与 Waiting(无限等待) 状态联系还是很紧密的, 比如Waiting(无限等待) 状态中wait方法是空参的,而timed waiting(计时等待) 中wait方法是带参的。 这种带参的方法,其实是一种倒计时操作,相当于我们生活中的小闹钟,我们设定好时间,到时通知,可是 如果提前得到(唤醒)通知,那么设定好时间在通知也就显得多此一举了,那么这种设计方案其实是一举两 得。如果没有得到(唤醒)通知,那么线程就处于Timed Waiting状态,直到倒计时完毕自动醒来;如果在倒 计时期间得到(唤醒)通知,那么线程从Timed Waiting状态立刻唤醒。

Guess you like

Origin www.cnblogs.com/hpcz190911/p/11909363.html