6 states of Java Multi-threading

Source: Internet
Author: User

Threads can have the following 6 states:

1. New (newly created)

When a new thread is created with the ' new ' operator, such as New (R), the thread has not started running. This means that its state is new.

When a thread is in the new state, the program has not started running code in the thread. There is some basic work to do before the thread runs.

2. Runnable (can be run)

Once the start () method is called, the thread is in the runnable state. A thread that can be run may or may not be running, depending on when the operating system gives the thread time to run (which is why the state becomes operational instead of running)

In fact, running threads are interrupted in order to get their threads to run the opportunity. The details of thread scheduling depend on the services provided by the operating system. The preemptive dispatch system gives each running thread a time slice to perform the task. When the time slice is exhausted, the operating system deprives the thread of the running power and gives another thread the opportunity to run. When the next thread is selected, the operating system considers the priority of the thread.

3. Blocked (blocked)

|

4. Waiting (Wait)

|

5.Timed Waiting (timed Wait)

When a thread is in a blocked or waiting state, it is temporarily inactive. It does not run any code and consumes the least resources. Until the thread scheduler activates it again. The details depend on how it has been inactive.

* When a thread attempts to acquire an internal object lock (rather than a lock in the Java.util.concurrent Library), and the lock is held by another thread, the thread goes into a blocking state. When the lock is freed by another thread, and the thread scheduler allows the thread to hold it, the thread becomes a non-blocking state.

* When a thread notifies another thread to notify the scheduler of a condition, it enters its own wait state. This occurs when you call the Object.wait method or the Thread.Join method, or you wait for lock or condition in the Java.util.concurrent library. In fact, the blocked state differs greatly from the waiting state.

* There are several methods that have a timeout parameter. Calling them causes the thread to enter the timed wait (timed waiting) state. This status will be maintained until the timeout expires or the appropriate notification is received. The methods with time-out parameters are Thread.Sleep and object.wait, Thrad.join, Lock.trylock, and Condition.await's timed version.

6. Terminated (terminated)

The thread is terminated for one of the following two reasons:

1) Natural death due to the normal exit of the Run method

2) Two accidental deaths due to an uncaught exception terminated by the Run method

6 states of Java Multi-threading

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.