The second way to implement using Multithreading:
First create a custom class
1 public classSellticketImplementsRunnable {2 //Define 100 Tickets3 Private intTicket = 100;4 5 public voidRun () {6 7 //assuming you've been selling tickets8 while(true){9 //in reality, there will be delays when buying tickets, so let the thread restTen Try { oneThread.Sleep (100); a}Catch(interruptedexception E) { - e.printstacktrace (); - } the if(ticket > 0){ -System.out.println (thread.currentthread (). getName () + "selling" + (ticket--) + "ticket"); - } - + } - } +}
To create a test class, the ticketing port is set at 3:
1 public classSellticket1demo {2 3 public Static voidmain (string[] Args) {4 5 //Create a Resource object6Sellticket st =NewSellticket ();7 8 //Create three thread objects9Thread St1 =NewThread (st, "ticket Gate 1");TenThread St2 =NewThread (st, "ticket Gate 2"); oneThread ST3 =NewThread (st, "ticket Gate 3"); a - //Start Thread - St1.start (); the St2.start (); - St3.start (); - - } + -}
When you start the program, you will see 2 problems:
First question: there will be 2 or 3 ticket outlets at the same time to sell the same ticket (ticket 1, 2, 3 at the same time sell the 90th Ticket)
The second question: to the last, even the ticket office sells No. 0, 1 tickets
Analysis Reason:
First question: there will be 2 or 3 ticket outlets at the same time to sell the same ticket (ticket 1, 2, 3 at the same time sell the 90th Ticket)
first, we assume that the first and second tickets are sold at the same time to sell the 100th Ticket:
Reason:
Because the thread default priority is 5, the same, to seize resources, here in order to facilitate the resolution, set st1 > st2.
Our ideal situation is: St1 sells the 100th ticket, St2 sells the 99th Ticket.
however, because each execution of the CPU must be an atomic Operation.
Atomicity: The simplest basic operations, such as i=100,System.out.println (i); This is the simplest basic operation, cannot be split, and I--is Not.
so, when the program goes to
System.out.println (thread.currentthread (). getName () + "being sold" + (ticket--) + "ticket");
A:CPU will first record the previous value (this is ticket=100)
B: then It's ticket--.
C: then output the original value (however, when the CPU executes the process, ST2 is breaking in)
D:ticket into 99.
E:st1 Sell 100th Ticket
F: because of the c-step, St2 broke in. So St2 sold the 100th Ticket.
Root cause: every execution of the CPU must be an atomic Operation.
The second question: to the last, even the ticket office sells No. 0, 1 tickets
We assume the worst case scenario: window 1 sells the 1th ticket, window 2 sells the No. 0 ticket, window 3 sells the first 1 tickets: (still assuming that the order of preemption to the resources is ST1 > St2 > St3)
Reason:
In Walking to
// T1 came in and rested, T2 came in and rested, T3 came in and rested,
Note: When you walk here, all 3 of them carry a variable: ticket = 1; Come in (the specific reason behind Said)
so, in executing
System.out.println (thread.currentthread (). getName () + "being sold" + (ticket--) + "ticket");
Results:
Window 1 sells the 1th ticket, ticket = 0;
Window 2 sells the No. 0 ticket, ticket =-1;
Window 3 for sale-1 tickets, ticket = 2;
Of course, This is the worst case scenario. It is possible to follow up on window 2, and it is possible to come in with Window 1.
But you can't put your expectations on top of the Possibilities.
Root cause: randomness and delay
Randomness (here is not known for a few threads to follow in) and delay causes (here is the judgment Delay)
In general, these two root causes are actually: thread security Issues.
The thread safety problem is not easy to appear in the ideal state, but once it appears, the impact on the software is very large.
so, we're going to solve the thread-safety Problem.
Java 22-11 multi-thread mock cinema ticket ticketing