Thread sync weird: asking for pointers

Source: Internet
Author: User

public class BUYER {
public static void Main (string[] args) {
Testthread a=new Testthread ();
Testthread b=new Testthread ();
A.setname ("A");
B.setname ("B");
A.start ();
B.start ();
}
}
Class Testthread extends Thread
{
static int rest=10;//number of votes left
int buy_1=7;//buy Ticket number

public void Run () {
int a=rest;
if (a>buy_1) {
A=a-buy_1;

Rest=a; ① Why the statement is placed here can never appear at the same time the success of the purchase of the case

System.out.println (Thread.CurrentThread (). GetName () + "\ n Success.");
 // ②rest=a;
}else
System.out.println (Thread.CurrentThread (). GetName () + "\ n fail.");

}
}

The handling of ticketing at ① is always true. The situation of ticketing at ② may occur at the same time as success?

Know what the solution of the mankind to seek guidance.

Thread sync weird: asking for pointers

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.