Four states of Java threads

Source: Internet
Author: User

A thread can have four states:

1, new, the thread has just been created, not executed

2, can be run (runnable), means that once the time shard mechanism has idle CPU cycles to a thread, that thread can start running immediately. Therefore, the thread may or may not be in operation, but once the condition permits, nothing can prevent it from running-it is neither "dead" nor "blocked".

3. Dead, after returning from its own run () method, a thread is "dead". You can also call Stop () to kill it, but create a violation-a subclass of error (that is, we don't usually capture it). Remember that a violation of the "throw" out should be a special event, not part of the normal program run. So it is not recommended that you use Stop () (in Java 1.2 is firmly opposed). There is also a destroy () method (which is never implemented) and should be avoided as much as possible because it is very arbitrary and does not unlock the object at all.

4, blocking (Blocked), the thread can run, but there is something blocking it. If the thread is in a blocked state, the scheduling mechanism can simply skip it and not allocate any CPU time to it. No action is taken unless the thread enters the "operational" state again. Why is it blocked because of:
(1) Call sleep (in milliseconds) to put the thread into a "sleep" state. This thread will not run for a specified period of time.

(2) The execution of the thread is paused with suspend (). The "operational" state is not returned unless the thread receives a resume () message.

(3) The execution of the thread is paused with wait (). The thread will not become "operational" unless it receives a nofify () or notifyall () message

(4) The thread is waiting for some IO (input and output) operations to complete.

(5) The thread attempts to invoke the "synchronous" method of another object, but that object is locked and temporarily unusable.

Four states of Java threads

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.