A problem exists: At this time four threads are started, then tickets is a member variable, that is, in a thread object to maintain its own tickets property, then there are a total of four copies.
Solution One:tickets uses staitc adornments so that each thread object is a shared copy of the property.
1.1
How to create a thread two
the second way to create a thread . Use Runnable Interface .
The code in this class is the definition of the task to be performed by the thread.
1: Defines the implementation of the Runnable interface
2: Override The Run method in the Runnable interface to put the code that runs the thread into the Run method
3: Thread object creation through the thread class
4: Pass the subclass object of the Runnable interface as the actual parameter to the Thread class construction method
5: Call thread Span style= "font-family: Arial" category start method to open thread and call runable interface subclass run method
Why do you want to pass the subclass object of Runnable thread run method belongs to runnable The sub-class object of the interface, so to have the thread execute the specified object's run method
package cn.itcast.gz.runnable; public , class demo1 { public Static void main (string[] args) { Myrun my = new Myrun (); Thread t1 = new thread (my); T1.start (); for ( int i = 0; i < $; i++) { System. Out . println ("main:" + i); } } } class myrun implements runnable { public , void run () { for ( int i = 0; I < 200; i++) { System. Err . println ("Myrun:" + i); } } } |
Understanding Runnable:
Threadclass can be understood as a worker,andRunnablethe object of the implementation class is the worker's job.(passing by construction method). RunnableThere is only one method in the interfaceRunMethod,The code defined in the method will be executed by the new thread.when we putRunnablethe subclass object passed to theThreadThe construction time,is actually to giveThreadGetRunMethod,is to give theThreada task.
Buy Tickets Example use Runnable Interface Implementation
after deliberately executing the thread in the code above, execute the Thread.Sleepto give the CPU to another thread, the method will have a non-runtime exception to handle, which must be try{} Catch(){}because the subclass cannot throw more exceptions than the parent class, there is no exception in the interface definition, and the implementation class cannot throw an exception.
Run the Discovery ticket number appears negative, showing the same ticket was sold 4 times.
The same problem has arisen. How to solve?
class Myticket implements Runnable { int tickets = 100; Public void Run () { while (true) { if (Tickets > 0) { Try { Thread. Sleep (100); } catch (Interruptedexception e) { E.printstacktrace (); } System. out. println (Thread. CurrentThread(). GetName () + " window @ Sales:" + Tickets + " number of tickets "); tickets--; } Else { System. out. println ("The ticket has been sold out ... "); break; } } } } Public class Demo6 { Public Static void Main (string[] args) { Myticket MT = new myticket (); Thread T1 = new thread (MT); Thread t2 = new thread (MT); thread t3 = new thread (MT); thread T4 = new thread (MT); T1.start (); T2.start (); T3.start (); T4.start (); } } |
Threading Simulation Tickets