Java Threads: Thread state

Source: Internet
Author: User

  Threads can create 6 states:
   New () newly created, Runnable (can run), Blocked (blocked), Waiting (wait), Timed waiting (timed Wait), Terminated (terminated)
1 new Thread
   When a thread is created with the new operator, such as new Thread (r), the thread has not started running. This means that its state is new. When a thread is in this state, the program has not started executing the code in the thread. There is some basic work to do before the thread runs.
2 can run threads
   once the start () method is called, the thread is in the runnable state, but it does not have to remain running. A running thread is interrupted in order to allow other threads to get the chance to run. The preemptive dispatch system gives each running thread a time slice to perform the task. When the time slice is exhausted, the thread is stripped of its running power and the opportunity is run to another thread. When the next process is selected, the priority of the thread is taken into account.
3 blocked threads and waiting threads
  
  as shown, when a thread attempts to acquire an internal object lock, and the lock is held by another thread, the thread goes into a blocked state. When all other threads release the lock, and the thread scheduler allows this thread to hold, it becomes non-clogging.  
when a thread waits for another thread to notify the scheduler of a condition, it enters the wait state itself. There are several methods that have a timeout parameter. Calling them causes the thread to enter a timed wait state. This status will be maintained until the timeout expires or the appropriate notification is received. The method with timeout parameters is timed by Thread.Sleep, object.wait, Thread.Join, Lock.trylock, condition.await.
when a blocked or waiting thread is reactivated (for example, because a timeout expires or a lock is successfully obtained), the scheduler checks to see if it has a higher priority than the currently running thread.
 4 Terminated threads
The reason the thread was terminated:
1) The Run method exits normally and dies naturally.
2) Accidental death due to an uncaught exception that terminated the Run method.

Java Threads: Thread state

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.