Multithreading (third day)

Source: Internet
Author: User

The thread must have a lock on the invoked instance to execute the Notify method, which is the same as the Wait method (also a rule)

Notify after the thread

The thread awakened by the notify is not restarted at the notify moment. Because at the moment of notify, the line layer executing notify is still holding the lock, so other threads cannot get the lock on that instance.

Notify how to select threads

Assuming that the Notify method is executed, the waiting thread in the wait set is not just one. The specification does not indicate which thread to select at this time. Whether you choose to wait for the first one in the thread list, then select or otherwise choose, will vary depending on the Java processing system, so it is best not to write the program's properties as it would be changed by the selected thread when writing the program.

Notifyall Remove all threads from the wait set

When using the Notifyall (notify all) method, all the threads in the wait set are taken out, assuming that the current situation is

Obj.notifyall ();

Will wake up all threads remaining in the wait set of instance obj

If the example method is written as Notifyall ();

Its meaning is the same as

This.notifyall ();

So the threads in the wait set for the instance of the method where this statement is located are all put out

Notify only wakes up one thread, and then the thread launches the wait set Notifyall wakes all the threads and leaves them all out of wait set

As with the Wait method and the Notify method, the thread must get the Notifyall method to invoke

The thread that wakes up starts to get the lock that was released when it just entered wait, so this lock is now in the hands of the thread that just executed the Notifyall method.

So even if all the threads have exited wait set, but they are still going to get the state they are in, there is still a block, wait until the thread that executes the Notifyall method releases the lock, and one of the lucky ones actually executes

If there is no locked thread to call wait notify Notifyall, an exception is thrown java.lang.IllegalMonitorStateException

Notify and Notifyall selection

The code written by Notifyall is more reliable than notify, and it's not easy to hang out.

Notify program processing speed is faster when thread is less

Wait Notify Notifyall is a method of the object class

Wait notify Notifyall are Java.lang methods of the object class, not the method inherent to the thread class

Obj.wait () is the wait set that puts the current thread in obj

Obj.notify () wakes a thread from the wait set in obj

Obj.notifyall () is the thread that wakes all of the wait sets in obj

is interpreted as an operation on the instance wait set, all instances will have wait set, so wait notify Notifyall is the method of the object class.

The three are really not the methods inherent in the thread class. But because the object class is an ancestor class, they are also methods of the thread class

Canceling a thread-handling interrupt

Interrupt Isinterrupt interrupted interruptedexception

Priority of the thread

SetPriority getpriority

Waiting for thread to end

Join

Be blocked and blocked

Sometimes threads can't go on for some reason. When thread A wants to execute the synchronized method, if other methods have acquired a lock on the same instance, thread A cannot move forward, a state called "Thread A is blocked",, and is the same as the blocked meaning

Multithreading (third day)

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.