Wait () notify () paired with synchronize

Source: Internet
Author: User

Always think that they move multi-threading, used as if to understand the principle of the same, in fact, is a step-by-step write their own do not know the principle of the code just.

Some concepts:

Monitor: Compare a monitor to a building, there is a special room in the building, there is some data in the room, the data can only be occupied by one thread at a time. This data can be enjoyed separately until a thread enters the room before leaving the house.

Enter the building---into the monitor

Into the building's room---get a monitor.

Occupy the room---hold a monitor

Leave the room---release the monitor.

Leave the building---exit the monitor

In conjunction with the state of the thread to understand these actions, no good description can express the feeling of these things, imaginative achievement here.

When a piece of code or data object is decorated with synchronize, this code or data is equivalent to the building façade that special room, the same time can only be a thread to enjoy, the other want to occupy the room of the threads are in accordance with some rules, such as the inside of the thread out, or the "rest" of the state to succeed ~

The thread holding the monitor temporarily exits the monitor in Wait (), enters the waiting state, first temporarily leaves, and then returns.

Notify () does not affect code execution, the waiting thread continues to hold the monitor after the current thread is paused or execution completes.

A simple chestnut

Package Studythread;public class Waitandnotify {class testwait extends thread{public void run () {Test ("wait Thread");}} Class Testnotify extends Thread{public void run () {Test ("notify Thread");}} public void Exe () {testwait tw = new testwait (); Tw.start (); Testnotify tn = new Testnotify (); Tn.start ();} Monitor public synchronized void Test (String str) {for (int i=0; i<10; i++) {if (i = = 5) {try {wait ();} catch (Interruptedexc Eption e) {e.printstacktrace ();}} Else{notify ();} SYSTEM.OUT.PRINTLN (str + "-------------" + i);}} public static void Main (string[] args) {new waitandnotify (). exe ();}}

Wait () notify () paired with synchronize

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.