Six states and switching Java threads

Java thread state is divided into six kinds:

1. Initial (NEW): create a new thread object, but has not been called start () method.
2. Run (RUNNABLE): Java thread will be ready (ready) and running (running) two states generally referred to as "run."
After the thread object is created, other threads (such as main thread) calls the start () method of the object. The thread state is located runnable threads in the pool, waiting to be selected thread scheduling, acquiring the right to use the CPU, this time in a ready state (ready). Ready state after obtaining thread CPU time slice becomes operational status (running).
3. blocked (BLOCKED): means the thread is blocked in the lock.
4. Wait (WAITING): This state is entered threads need to wait for other threads to make some specific action (notification or interrupt).
The time-out period (TIMED_WAITING): This state is different WAITING, it can return itself after a specified time.

6. Termination (TERMINATED): the thread has finished.

Thread state diagram


1. initial state

Implement Runnable and inheritance Thread class may get a thread, new one instance out of thread enters the initial state.

2.1 Ready

  • Ready just say you qualify to run, the scheduler does not pick you, you will never be ready.
  • The calling thread's start () method, the thread into the ready state.
  • The current thread sleep () method ends, another thread join () end, waiting for user input is complete, a thread lock to get the objects, these threads will enter the ready state.
  • Current thread time slice runs out, calling the current thread yield () method, the current thread into the ready state.
  • Lock thread pool get the object lock, enter the ready state.

2.2. Running state

A thread scheduler selecting a pool from a running thread in a time as the current thread state. This is also the only way to thread into the running state.

3. blocked

Blocking state is blocked in the status of the thread enters the synchronized keyword or code block modification method (lock acquisition).

4. Wait

Thread in this state will not be assigned CPU execution time, they have to wait to be explicitly resumed, otherwise they will be in a state of waiting indefinitely.

5. Timeout waiting

Thread in this state will not be assigned CPU execution time, but without waiting for other threads to be displayed indefinitely wake up, they will wake up automatically after a certain time.

6. Termination state

  • When the thread run () method when complete, or main thread of the main () method is completed, we believe that it terminated. The thread object might be alive, but it has not a single thread of execution. Upon termination of the thread, it is final.
  • Call start on a thread terminates () method throws an exception java.lang.IllegalThreadStateException.

Waiting queue

  • Before calling the obj of wait (), notify () method, must be obj lock, which is to be written in (obj) code segment synchronized.
  • And the step of waiting queue associated with
  • 1. A thread 1 acquires the object lock, is using the object A.
  • 2. Thread 1 calls the object A wait () method.
  • 3. Thread 1 to release the lock object A, and immediately enter the waiting queue.
  • 4. The lock pool inside the lock object A competition for the object.
  • 5. A thread locking object 5 is obtained, the process proceeds synchronized block, using the object A.
  • 6. 5 thread calls the object A notifyAll () method wakes up all threads, all threads to enter the synchronization queue. If a thread calls the object A 5 notify () method, then wake up a thread, do not know who will wake up, wake up that thread into the synchronous queue.
  • 7.notifyAll () method where synchronized end, 5 thread releases the lock object A.
  • 8. The competition thread synchronization object lock queue, but when will grab the thread 1 do not know.

Synchronization queue status

  • When the current thread synchronization method of the object A wants to call, found the lock object A is occupied by another thread, then the current thread into the synchronous queue. In short, the synchronization queue which put all want to compete for the object lock thread.
  • When a thread is a wake-up another thread 2, a thread into the synchronous queue, to compete for the object lock.
  • Synchronous queue is only conceptual in synchronous environment, an object corresponding to a synchronous queue.

Comparison of several methods

  • Thread.sleep (long millis), the current thread must call this method, the current thread into the TIMED_WAITING state, but does not release the object lock, automatic wake up after millis thread into the ready state. Role: The best way to give the opportunity to perform other threads.
  • Thread.yield (), the current thread must call this method, the current thread to give up CPU time slice acquired, but does not release the lock resources by running state to the ready state, choose to let OS thread again. Role: let the same priority thread execution by turns, but does not guarantee that will take turns to perform. We can not guarantee the actual yield () to achieve the purpose of concessions, because concessions threads may also be selected again thread scheduler. Thread.yield () does not cause obstruction. This method sleep () is similar, but not specified by the user to pause long.
  • t.join () / t.join (long millis), thread the current method call join other thread t, the current thread into the WAITING / TIMED_WAITING state will not release the current thread already holds the object lock. Thread t finished or millis time to, the current thread into the ready state.
  • obj.wait (), the current thread calls the object's wait () method, the current thread releases the object lock, into the waiting queue. Rely notify () / notifyAll () Wake or wait (long timeout) timeout time to automatically wake.
  • obj.notify () wake up a single thread waiting on this object's monitor, the choice is arbitrary. notifyAll () wakes up all threads waiting on this object's monitor.
Published 964 original articles · won praise 11 · views 30000 +

Guess you like

Origin blog.csdn.net/xiaoyaGrace/article/details/105400462