Why Java wait/notify must be used with synchronized, what the JVM actually does

Source: Internet
Author: User
Tags set time

This topic is proposed is the original thread to solve the idea of concurrency. Currently resolving thread concurrency, which can be a lock interface combined with condition concurrency problem has always been a thread essential topic.

Java is the first mainstream programming language with built-in support for multithreading. Before Java5, the support of multithreading is mainly implemented by synchronizing the block structure (synchronized with Wait,notify,notifyall). Java5 introduced the Java.util.concurrent package, which provides a higher level of support for multithreaded programming.

Today, let's talk. In Java, threads provide us to resolve concurrency. Methods of synchronization

You can typically use synchronized and notify,notifyall and the wait method to implement data transfer and control between threads. For object obj:

    • Obj.wait (): The invocation of the method, which causes the execution thread calling the method (T1) to discard obj's object lock and block until another thread calls the Notifyall method of obj, or the other thread invokes the Notify method of obj and the JVM chooses to wake Up (T1). The awakened thread (T1) still blocks in the wait method, competing with other threads for the object lock on obj until it obtains the object lock of obj again before it can be returned from the wait method. (In addition to the Notify method, wait also has a time parameter version, after waiting for more than the set time, the T1 thread will be awakened, into the queue for the Obj object lock, the other interrupt can be directly out of the Wait method)
    • Obj.notify (): The invocation of the method wakes one of them from all the threads that are waiting for the Obj object Lock (the selection algorithm relies on different implementations), and the awakened thread is now joined in contention for the Obj object lock, However, the execution thread of the Notify method does not release obj's object lock at this time, but is freed when it leaves the synchronized code block. So after the Notify method, all the other threads that were awakened, waiting for the Obj object lock, are still blocked until the synchronized code block ends.
    • Obj.notifyall (): The difference from notify is that the method wakes up all threads that are waiting for the Obj object lock. (But at the same time, only one thread can have an object lock on obj)

Note that the calls to the Wai,notify and Notifyall methods must be within the corresponding synchronized code block

The thread is the path to the program execution. Multithreading, it is not difficult to understand, the program executes a number of paths, they execute independently, but there is a great connection

Wait/notify literally means to wait and tell

 Packagecom.huojg.test; Public classThreada { Public Static voidMain (string[] args) {threadb b=Newthreadb (); B.start ();//starting another thread in the main threadSystem.out.println ("B is start ....")); //What's the meaning of B in parentheses, it should be well understood.        synchronized(b) {Try{System.out.println ("Waiting for B to complete ..."); B.wait ();//What does this sentence mean, exactly who waits?System.out.println ("THREADB is completed. Now back to main thread "); } Catch(Interruptedexception e) {}} System.out.println ("Total is:" +b.total); }}classThreadbextendsThread {intTotal ;  Public voidrun () {synchronized( This) {System.out.println ("THREADB is running..");  for(inti = 0; I <= 100; i++) { Total+=i; } System.out.println ("Total is" +Total );        Notify (); }    }}
 for B to complete ... THREADB is completed. now back to main threadtotal is: 5050

From the results of the program run it is not difficult to understand wait/notify, wait is to let the wait method of the object waits, temporarily lock the object to let out, to other hold the lock object with, other objects used to tell (notify) wait for the object can continue to execute, This is the whole process. Wait/notify is used primarily when one thread waits for another thread to finish executing, and then gets the result of the execution.

Why Java wait/notify must be used with synchronized, what the JVM actually does

Related Article

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.