Java base 005 thread synchronized and polling __java

Source: Internet
Author: User
The wait/notify mechanism is designed to avoid the performance loss caused by polling.
To clarify the truth, we use the classic example of "library books" to explain. A book can only be lent to one person at a time. Now there is a book, the library has borrowed the book John.
Under the simple synchrnozed synchronization mechanism, if Li Si wants to borrow, first must go to the library to check whether the book has returned. Lee Four is a impatient person, he went to the library every day to check, and John read slowly, after half a month to return the book, the result dick in this half a month all white ran, wasted a lot of traffic fares
And if the use of wait/notify mechanism, dick not white busy. The first time he went to the library to find the book has been borrowed, he went home quietly waiting (wait), John the book Back, notice (notify) Dick, Dick to the library to get the book. Throughout the process, Dick did not run white, did not waste money.
Back to the computer world:

Book-critical resources, need to access mutually exclusive
John, Dick--Two competing threads
Take the bus to the library to check the book-poll
Fare--CPU space
Waiting-Wait
Inform the next borrower-Notify


In other words, if the use of simple synchonized mechanism to achieve mutual exclusion, will cause the thread to initiate polling, if the N polling did not succeed, it produced n times the CPU space waste; If you add the wait/notify mechanism, you can avoid these unnecessary polling, save CPU consumption.




Example:
Package Sun;


public class T3 {
public static void Main (String s[]) {
Q q = new Q ();
New put (q);
New Get (Q);
}
}


Class Q {
private int n;
Private Boolean iswait = false;


synchronized void put (int n) {
if (iswait) {
try {
Wait ();
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
THIS.N = n;
System.out.println ("put=" + N);
Iswait = true;
Notify ();
}


synchronized void get () {
if (!iswait) {
try {
Wait ();
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
System.out.println ("get=" + N);
Iswait = false;
Notify ();
}
}


Class put implements Runnable {
Q q;


Public put (q q) {
THIS.Q = q;
New Thread (this, put). Start ();
}


public void Run () {
int i = 0;
while (true) {
Q.put (i++);
}
}
}


Class Get implements Runnable {
Q q;


Public get (q q) {
THIS.Q = q;
New Thread (this, ' get '). Start ();
}


public void Run () {
while (true) {
Q.get ();
}
}
}
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.