April 24, 2016 _java Learning Notes _ Multithreading Three _ thread communication between threads

Source: Internet
Author: User

1, the 14th day of the content of teacher, the communication between the threads. It is probably using a series of functions such as Wait (), notify () to control the CPU execution qualification and execution of each thread, and to switch between threads to achieve the purpose of inter-thread communication through the right time.

The approach involved:

Wait (): The thread waits for the thread to wait, and the threads of Wait () are stored in the thread pool until they are awakened. Can only be called in a synchronous method.

Notify (): Randomly selects a thread that calls the wait method on the object to unblock its state. Can only be called in synchronous methods and in synchronous code blocks.

Notifyall (): Touches all the blocking states of threads that call the Wait () method on the object. Can only be called in a step method and in a synchronized code block.

The emphasis is on being called only in synchronization, because the thread that is waiting in a synchronous lock can only be unblocked from another thread in the same synchronization lock. It is also important to note that these methods are all defined in the object class, because these methods are monitor methods, so what is the monitor? In the description I see now, the thread's monitor is the lock held by the thread, which is an object associated with the Synchronized keyword. Because a lock can use any object, the method of locking must be defined in object.

1 classOutputImplementsRunnable2 {3Demo R;//objects that need to be output4Output (Demo R)//constructor, passing in a resource object from the main function as a common processing content5     {6          This. R =R;7     }8      Public voidRun ()9     {Ten          while(true) One         { A             synchronized(r)//synchronous lock to ensure that the input thread does not modify the resource at the time of output -             { -                 if(!r.flag)//if it's fake, then wait. the                     Try{r.wait ();}Catch(Interruptedexception e) {}//Wait, exception handling is required -System.out.println (r.name+ "..." +r.sex);//If flag is true, it will enter this step and output the resource -R.flag =false;//set flag to False to allow output thread to wait -R.notify ();//wake up another thread before waiting, note that wake up here should be random, but there are only two threads, so it's okay +             } -         } +     } A}

The output threads in a video are used as instances of the wait () and notify () methods.

April 24, 2016 _java Learning Notes _ Multithreading Three _ thread communication between threads

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.