Thread Detail 3: The state of threads and several simple methods

Source: Internet
Author: User
1 Thread state typeNew State (new): A newly created thread object. Ready state (Runnable): After the thread object is created, other threads call the object's start () method. The thread of this state is in the running thread pool, becomes operational, and waits for access to the CPU. Running state (Running): A ready-state thread acquires the CPU and executes the program code. Blocking state (Blocked): Blocking state is the thread for some reason to give up the use of CPU, temporarily stop running. Until the thread enters the ready state, there is an opportunity to go to the running state. The blocking situation is divided into three types:
(i), wait for blocking: The running thread executes the wait () method, and the JVM puts the thread into the waiting pool.
(ii), synchronous blocking: When a running thread acquires a synchronized lock on an object, the JVM puts the thread into the lock pool if the synchronization lock is occupied by another thread.
(iii), other blocking: The running thread executes the sleep () or join () method, or when an I/O request is made, the JVM puts the thread into a blocking state. When the sleep () state times out, join () waits for the thread to terminate or timeout, or the I/O process completes, the thread is back in ready state. Death status (Dead): The thread finishes or exits the run () method because of an exception, which ends the lifecycle.

2 Sleep

Lets the currently executing thread hibernate (suspend execution) within the specified number of milliseconds, which is affected by the system timer and scheduler precision and accuracy. The thread does not lose ownership of any monitor. 3 Yield

The Thread.yield () method pauses the currently executing thread object and executes other threads. What yield () should do is let the current running thread go back to the operational state to allow other threads with the same priority to get a chance to run. Therefore, the purpose of using yield () is to allow appropriate rotation between threads of the same priority. However, in practice there is no guarantee that the yield () will achieve the concession because the thread of compromise may also be selected again by the thread scheduler.

Conclusion: Yield () has never caused a thread to go to a wait/sleep/block state. In most cases, yield () will cause the thread to go from the run state to the operational state, but it may not work. 4 Join

In many cases, the main thread generates and moves the child thread, if there is a large number of time-consuming operations to be performed in a child thread, the main thread will often end before the child thread, but if the main thread finishes processing other transactions, it needs to use the processing result of the child thread, which means that the main thread needs to wait for the execution of the child threads to finish. What to do at this time.

When you want to see the first time that the blogger is in touch with multiple threads, I don't know if there is a join () method, which is to let the main thread sleep for 10 seconds, and guess that time the child thread has finished calculating. is not very stupid, very inefficient. This time join () the horse.

"Wait for this thread to terminate", what needs to be understood here is that the thread is referring to the main thread waiting for the child thread to terminate. That is, the code that follows the join () method is called on the child thread and cannot be executed until the child thread has finished. Example of a comparison:

Code 1:

public class MyThread1 extends Thread {

    @Override public
    void Run () {
        super.run ();
        String name = This.getname ();
        for (int i = 0; i < 6; i++) {
            System.out.println (name);
            try {
                thread.sleep;
            } catch (Interruptedexception e) {
                e.printstacktrace ()

    }}} public static void Main (string[] args) {
        MyThread1 t1 = new MyThread1 ();
        MyThread1 t2 = new MyThread1 ();
        T1.start ();
        T2.start ();
    }

Output

Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1 Thread-0 Thread-1
Thread-0
Thread-1
Thread-0
Thread-1

Code 2:

      
        ....... T1.start ();
        try {
            t1.join ();
        } catch (Interruptedexception e) {
            e.printstacktrace ();
        }
        T2.start ();
      ...... ......

Output

Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0 Thread-1 Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
5 without suspend

To say that the thread is paused, then, as in the previous article Bovenri the thread stop, the outdated method is not: Thread.Suspend and Thread.Resume. These two methods are a pair, in which suspend has a deadlock tendency, so it has been "outdated". Why, then? Look at the source code comments:

     * If The thread is alive, it's suspended and makes no further
     * progress unless and until it is resumed.

That is, once the suspend is invoked, the thread is immediately frozen, and there is "no next step", which is the problem: if the target thread hangs with a lock on the monitor that protects critical system resources, no thread will be able to access the resource until the target thread restarts. If the thread that restarted the target thread wanted to lock the monitor before calling resume, a deadlock would occur. This type of deadlock usually proves itself to be a "frozen" process.

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.