Java synchronized instance

Source: Internet
Author: User

The following is an example of a multi-thread ticket buying error:

Public class SaleTicketMutiThread implements Runnable {
Int tickets = 100;
Int temp = tickets;
Boolean flag = true;
@ Override
Public void run (){
While (flag ){
If (tickets> 0 ){
Try {
Thread. sleep (30 );
} Catch (InterruptedException e ){
E. printStackTrace ();
}
Sale ();
} Else {
Flag = false;
System. out. println (Thread. currentThread (). getName () + "sold out ");
}
}
}
Public synchronized void sale (){
Tickets --;
System. out. println (Thread. currentThread (). getName () + "sold"
+ (Temp-tickets) + "sheets," + tickets + "tickets" left in the system ");
}
/**
* @ Param args
*/Www.2cto.com
Public static void main (String [] args ){
SaleTicketMutiThread st = new SaleTicketMutiThread ();
New Thread (st, "Window 1"). start ();
New Thread (st, "window 2"). start ();
New Thread (st, "window 3"). start ();
New Thread (st, "Window No. 4"). start ();
}
}

Finally, the running result will show a negative number:
......
99 tickets have been sold in Window 4, and the system has 1 ticket left.
Window 1 has sold 100 tickets and the system has 0 tickets left.
Window 1 sold out
Window 3 has sold 101 tickets, and the system has one ticket left.
The fourth window has sold 102 tickets, and the system has 2 tickets left.
Windows 2 has sold 103 tickets and the system has 3 tickets left.


Analysis:

When thread 1 is executed to sleep, thread 2 has the opportunity to occupy the cpu (or thread 3 or thread 4), and thread 2 is executed to sleep, thread 3 has the opportunity to occupy cpu (or thread 4), thread 4 has the opportunity to occupy cpu from thread 3 to sleep, and thread 1 may just wake up when thread 4 to sleep, continue to run down, call the sale method, and execute tickets --. Other threads also wake up and execute tickets --. When tickets = 1, assume that thread 1 is running, judge 1> 0. The condition is true. When thread 1 is executed to sleep, let the threads 2, 3, and 4 have the opportunity to execute. At this time, the value of tickets is still 1, because the condition judgment statement is sleep, all threads have the opportunity to enter the condition block, so each thread calls the sale method, even if tickets = 0, tickets will also be reduced three times. That is, the number of votes may be-1,-2,-3.

Solution:

After the ticket is received, sleep is synchronized with the number of votes minus one, so that you can avoid other window ticket obtaining actions because the last ticket is sold but the number of votes is not reduced by one.
Run successfully after modification:

Public class SaleTicketMutiThread implements Runnable {
Int tickets = 100;
Int temp = tickets;
Boolean flag = true;
@ Override
Public void run (){
Ticket ();
}
Public synchronized void ticket ()
{
While (flag ){
If (tickets> 0 ){

Try {
Thread. sleep (30 );
} Catch (InterruptedException e ){
E. printStackTrace ();
}
Sale ();
} Else {
Flag = false;
System. out. println (Thread. currentThread (). getName () + "sold out ");
}
}
}
Public void sale (){

Tickets --;

System. out. println (Thread. currentThread (). getName () + "sold"
+ (Temp-tickets) + "sheets," + tickets + "tickets" left in the system ");
}
/**
* @ Param args
*/
Public static void main (String [] args ){
SaleTicketMutiThread st = new SaleTicketMutiThread ();
New Thread (st, "Window 1"). start ();
New Thread (st, "window 2"). start ();
New Thread (st, "window 3"). start ();
New Thread (st, "Window No. 4"). start ();
}
}

The final result is as follows:
......
99 tickets have been sold in Window 4, and the system has 1 ticket left.
Window 1 has sold 100 tickets and the system has 0 tickets left.
Window 1 sold out

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.