JVM thread thread state and tristate

1, thread tri-state: ready state , running state , blocking state

img

2, six states in the JVM

  • NEW (New State): a state in which the thread has not been started.

  • RUNNABLE (run state): Thread state can run threads may be running, may also be waiting for processor resources.

  • The BLOCKED (lock blocked): a state in which the monitor lock thread blocked waiting.

    Use: When a thread tries to acquire the lock, but this time the lock is held by another thread, the thread entered the BLOCKED state, when the thread lock to get into the RUNNABLE the state.

  • The WAITING (wait indefinitely, blocking): The state of the thread in which the waiting time is not specified.

    Use: When calling Object.join (), enters this state when Object.wait () method, a thread is in this state, only to be awakened by another thread, but can not wake up on their own initiative, another thread calls notify () or notifyAll () to wake up the thread.

  • TIMED_WAITING (timed wait blocking.): Specifies the state of waiting time in which the thread.

    Use: Thread.sleep (long), enters this state when Object.join (long) or Object.wait (long) method, the time until a timeout or receives a wakeup notice.

    • Note: wait (0) is their own awakening, such as when the end of the Thread will automatically wake.
  • TERMINATED (end state): state of the thread has completed execution.

3、join和wait

join action: join a major role in the method of the Thread class is synchronized, so that it can be executed in parallel between the thread becomes the serial execution. When you call a thread join B () method in the thread A, B said that only when the thread is finished when, A thread can continue. join () method must be called only makes sense after (0 method is called a thread start.

join Principle: wait method is to call the corresponding thread waits operations such as A thread join method calls the thread B, is equivalent to a call wait method B A thread in the thread, when executing the thread B (or arrival waiting time), B thread will automatically wake up calls itself notifyAll method a thread, so as to achieve synchronization.

  • The implementation is based on the underlying join the wait.

  • wait communication between threads is commonly used signals, the role is to allow the thread to temporarily stop running, waiting for other threads using notify to wake up or wake themselves up to a certain condition.
  • It is a local wait method, which belongs to the class Object, which is an internal JVM implementation underlying implementation is based on the monitor lock object monitoring.

  • Thread class join method is, not the underlying native method.

  • join method synchronized keyword modification, it is thread-safe.

  • join the three overloaded methods are mainly based on the join (long millis) method, so we focused on processing logic of this method, the method is as follows

    • Millis parameter is less than 0, throws IllegalArgumentException ( "timeout value is negative") Exception
    • Millis parameters equal to 0, judgment calls join the thread (assumed to be A) is alive, do not survive the operation is not performed, if alive, would call wait (0), blocked join method, waiting A thread executing the method at the end of the join.
    • Millis parameter is greater than 0, the judge invoked join A thread is alive, do not survive the operation is not performed, if alive, would call wait (long millis), blocked join method, wait time and then proceed to join method.

    Since the join is synchronized modified synchronization method, so there will be join (long millis) blocking time exceeds the value of millis.

Guess you like

Origin www.cnblogs.com/aric2016/p/11496090.html