Wait/notify of Java Threading Interaction

Source: Internet
Author: User

It is well known that Java supports multi-threading, there are multiple threads of multi-threaded interaction, there are many ways of interaction, such as by sharing data, or by providing the amount of lock signal, and so on, by using wait and notify to implement thread interaction.

To use a good thread of wait and notify to first understand the state of the thread, the state of the thread can be roughly divided into several, create------------------

A thread through new to call start completes the creation to ready, after this is waiting for the CPU allocation of resources to run, blocking state is the thread running state, there are a number of threads blocking situation, there are many, here is a simple list of

1> Lock Blocking

2> Call Wait Blocking

3> call Join Blocking

4> Call Sleep Blocking

Of course, threads can also be returned from the running state to the ready state, which is implemented by invoking the Yeild method of the thread class, which is briefly explained, wait and notify and Notifyall are provided in the super-parent class object, which means that the class's

, while Sleep/join/yeild is provided by the thread class, which is the thread's

From the above, wait and notify can only be used in the object lock, can not be used with the shared lock can only be used with the synchroized one of the key, that is, wait and notify before you must obtain the object's lock, when the call to wait,

The thread is placed inside the wait queue of the object monitor, and the lock is released, waiting for other threads to call notify from the lock Object Monitor's wait queue, to get a thread to execute, remembering that synchronized is the object lock, which means that she wants to lock the whole object.

Through the above we have roughly understood, wait and notify knowledge points, below please see example, simple producer Consumer example

1  Public classTest {2 3     Private Staticlist<integer> queue =NewArraylist<>();4     5     6     /**7 * Producer8      * @paramn void9      *     Ten      */  One      Public synchronized voidProducerintN) { ASystem.out.println ("Insert data" +n); - Queue.add (n); -          the         if(Queue.size () ==1) -              This. Notify (); -     } -      +     /** - * Consumer +      * @returnint A      *      at      */  -      Public synchronized intconsumer () { -             Try { -                 if(Queue.isempty ()) -                      This. Wait (); -}Catch(interruptedexception e) { in e.printstacktrace (); -             } to         intresult = Queue.get (0); +Queue.remove (0); -         returnresult; the     } *      $      Public Static voidMain (string[] args) {Panax Notoginseng         FinalTest test =NewTest (); -          theThread Thread1 =NewThread (NewRunnable () {//Line Process +              A @Override the              Public voidrun () { +Random random =NewRandom (); -                 Try { $                      while(true){ $Test.producer (Random.nextint (1000)); -Thread.Sleep (2000); -                     } the}Catch(interruptedexception e) { - e.printstacktrace ();Wuyi                 } the             } -         }); Wu          -Thread thread2 =NewThread (NewRunnable () {//Consumer Threads About              $ @Override -              Public voidrun () { -                 Try { -                      while(true){ ASystem.out.println ("Select data is" +Test.consumer ()); +Thread.Sleep (1000); the                     } -}Catch(interruptedexception e) { $ e.printstacktrace (); the                 } the             } the         }); the          - Thread1.start (); in Thread2.start (); the     } the}

The above is a personal understanding, such as the wrong place, you are welcome to criticize correct, greatly appreciated

Wait/notify of Java Threading Interaction

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.