JAVA multithreading -- thread

Thread state diagram (from the network)

 

The thread includes the following five states.
1.  New state (New)          : After the thread object is created, it enters the new state. For example, Thread thread = new Thread().
2.  Ready state (Runnable) : Also known as "executable state". After the thread object is created, other threads call the start() method of the object to start the thread; or the thread acquires the lock, ends the sleep, and ends the join to end the Blocked state. Threads in the ready state can get CPU time slice execution at any time.
3.  Running state (Running)  : The thread obtains the CPU permission to execute. It should be noted that a thread can only enter the running state from the ready state.
4.  Blocked state (Blocked)   : The blocked state is that the thread gives up the right to use the CPU for some reason and temporarily stops running. Until the thread enters the ready state, there is no chance to go to the running state. There are three kinds of blocking situations:
    (01) Waiting for blocking -- By calling the thread's wait() method, the thread waits for the completion of a certain job.
    (02) Synchronization blocking -- A thread fails to acquire a synchronized synchronization lock (because the lock is occupied by other threads), and it will enter a synchronized blocking state.
    (03) Other blocking -- The thread will enter the blocking state by calling the thread's sleep() or join() or when an I/O request is issued. When the sleep() state times out, join() waits for the thread to terminate or times out, or when the I/O processing is complete, the thread re-enters the ready state.
5.  Dead state (Dead)    : The thread finishes executing or exits the run() method due to an exception, and the thread ends its life cycle.

What about the BLOCKED/TIMED_WAITING/WAITING status in the stack information seen by the jstack command? jstack will output thread stack information when the thread is at safepoint, so the thread information printed by the same stack file is not synchronized, not the information at the same time point.

    RUNNABLE  Thread running or I/O waiting

    The BLOCKED  thread is waiting for the monitor lock (synchronized keyword), but not waiting for the ReentrantLock, sleep will hold the lock, and wait will not .

public class BlockedTest {
    public static void main(String[] args) {
        final Object lock = new Object();
        new Thread() {
            public void run() {
                synchronized (lock) {
                    try {
                        Thread.sleep(1000 * 1000);
                    } catch (InterruptedException e) {
                    }
                }
            }
        }.start();

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
        }

        synchronized (lock) {
            try {
                Thread.sleep(30 * 1000);
            } catch (InterruptedException e) {
            }
        }
    }
}
View Code

Main thread BLOCKED state

The lock holding thread is in the TIME_WAITING state, waiting for the sleep to end.

 

    TIMED_WAITING  The thread is waiting to wake up, but a time limit has been set. With Lock.tryLock(timeout, timeUnit), this method will also see the TIMED_WAITING state

public static void timedWaiting() {
      final Object lock = new Object();
      synchronized (lock) {
          try {
              lock.wait(10 * 1000);
          } catch (InterruptedException e) {
          }
     }
}

 

    The WAITING  thread is waiting indefinitely to wake up, waiting without timeout, and must wait for lock.notify() or lock.notifyAll() or receive an interrupt signal to exit the waiting state. Similarly, the no-argument method call of ReentrantLock.lock() will also change the thread state to WAITING.

public static void waiting() {
      final Object lock = new Object();
      synchronized (lock) {
          try {
                lock.wait();
           } catch (InterruptedException e) {
         }
     }
 }
public class ReentrantLockTest {

    public static void main(String[] args) {
        final ReentrantLock lock = new ReentrantLock();
        new Thread() {
            public void run() {
                lock.lock();
                try {
                    Thread.sleep(1000 * 1000);
                } catch (InterruptedException e) {
                }
                lock.unlock();
            }
        }.start();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
        }
        lock.lock();
    }
}
ReentrantLock

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325891731&siteId=291194637