Java-Preliminary Understanding-the 14th chapter-the difference between multithreading-wait and sleep

Source: Internet
Author: User

One.

Wait and sleep methods are somewhat similar, and we now want to describe them and differentiate them.

The wait method is in the object class, and there are two forms, wait () and wait (long timeout), and we focus on the latter.

The parameter indicates that the wait method can wake up.

The difference between waiting and sleeping, sleep can wake up, but waiting may be endless.

The two are an object method and one is the thread method.

Sleep can not wake up, do not need to be called. Sleep holding the lock, others can not enter.

Now let's talk about some interesting ideas that might subvert the past.

The two are the same lock, t0,t1,t2 three threads all rushed into the show method, all down. T0 get the lock this, enter the synchronization code block, wait. Wait here for a wait, enter the thread pool, then release the execution, and release the lock. Because in the other method, there may be notify, otherwise T4 how to save T0~t2,

T0 a hang, T1,t2,t4 will rob execution right. T1 get executive power and lock, come in also wait, the same T2 will hang in this.

Now to say this is the problem, T4 come in, Notifyall, the above three threads all live. This is problematic, in the synchronization, a moment can only have one thread to execute, now there are three living inside, how to do?

Not in the outside to be safe, and now all come in, this is also the common code, a number of statements, the three threads one operation is hung,

Three threads are alive, waiting for execution, and if the CPU switches to T0, then T0 is not necessarily running. Because all the threads are in sync, in sync, you want to run, you have a qualification, you have to hold the lock. T4 a notifyall, the top three threads are alive, only to indicate that they are qualified, but T4 still hold the lock? T4 has not come out yet, T4 from the synchronization of a out, the lock is put. This one, the top three threads have a cut-to-execute right, and it gets the lock. If T0 gets a lock, it executes, and T1 and T2 are impossible, and they cannot execute unless T0 goes out.

Synchronization there can only be one thread executing at a time, but there is more than one live thread. Who takes the lock who executes.

Java-Preliminary Understanding-the 14th chapter-the difference between multithreading-wait and sleep

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.