1. First of all, two ways to create a thread
One way is to inherit the thread class and override the run () method
1 Public classMyThreadextendsthread{2 @Override3 Public voidrun () {4 //TODO auto-generated Method Stub5 6 }7 }8 //Thread Usage9MyThread MT =NewMyThread ();//Creating ThreadsTenMt.start ();//Start Thread
Another way is to implement the Runnable interface
1 Public classMyThreadImplementsrunnable{2 @Override3 Public voidrun () {4 //TODO auto-generated Method Stub5 6 }7 }8 //Thread Usage9MyThread MT =NewMyThread ();TenThread thread =NewThread (MT);//Creating Threads OneThread.Start ();//Start Thread
2. Two ways to create a thread comparison
1th: Through the creation of threads can be seen, one is to inherit one is to implement the interface, but Java can only inherit a parent class, it is possible to implement a feature of multiple interfaces, so the use of runnable method can avoid the thread mode due to Java single inheritance caused by the flaw.
2nd: runnable code can be shared by multiple threads (thread instances) and is suitable for multiple threads to handle uniform resources.
Example: Simulated sell tickets, assuming that there are 5 tickets left, two ways to create a threading simulation
Ticketthread.java//The case of using inheritance to simulate 3 windows selling 5 tickets
1 Public classTicketthread {2 Public Static voidMain (string[] args) {3 //created 3 threads, simulated three windows sell tickets4MyThread MT1 =NewMyThread ("Thread One");5MyThread mt2 =NewMyThread ("Thread Two");6MyThread MT3 =NewMyThread ("Thread Three");7 8 //Start Thread9 Mt1.start ();Ten Mt2.start (); One Mt3.start (); A } - - } the - classMyThreadextendsthread{ - Private intnum = 5;//Simulation also has 5 tickets left - PrivateString name;//used to represent the thread name + - PublicMyThread (String name) { + This. Name =name; A } at - @Override - Public voidrun () { - while(num > 0){ -num--; -System.out.println (name+ "sold a ticket, the remaining votes are" +num); in } - } to}
The result of the operation is:
1 The thread sold one ticket at a 4 of the remaining votes2 The thread sold one ticket at a 3 of the remaining votes3 The thread sold one ticket at a 2 of the remaining votes4 The thread sold one ticket at a 1 of the remaining votes5 The thread sold one ticket at a 0 of the remaining votes6 thread two sold a ticket with 4 remaining votes7 thread two sold a ticket with 3 remaining votes8 thread two sold a ticket with 2 remaining votes9 thread two sold a ticket with 1 remaining votesTen thread two sold a ticket with 0 remaining votes One thread three sold a ticket, the remaining number of votes is 4 A thread three sold a ticket, the remaining number of votes is 3 - thread three sold a ticket, the remaining number of votes is 2 - thread three sold a ticket, the remaining number of votes is 1 theThread three sold a ticket, the remaining number of votes is 0
You can see that 15 tickets have been sold, and 5 tickets have been turned into each thread.
Ticketrunnable.java//with runnable implementation
1 Public classtickerrunnable{2 Public Static voidMain (string[] args) {3MyThread MT =NewMyThread ();4 //also create 3 threads5Thread T1 =NewThread (MT, "thread One"));6Thread t2 =NewThread (MT, "thread Two");7Thread t3 =NewThread (MT, "thread three");8 //Start Thread9 T1.start ();Ten T2.start (); One T3.start (); A } - - } the - classMyThreadImplementsrunnable{ - Private intnum = 5;//Simulation also has 5 tickets left - + @Override - Public voidrun () { + while(num > 0){ Anum--; atSystem.out.println (Thread.CurrentThread (). GetName () + "sold a ticket with the remaining votes" +num); - } - } -}
The result of the operation is:
1 The thread sold one ticket at a 4 of the remaining votes 2 thread three sold a ticket, the remaining number of votes is 3 3 thread three sold a ticket, the remaining number of votes is 1 4 thread two sold a ticket with 0 remaining votes 5 Thread One sold a ticket, the remaining votes of 2
This shows that multiple threads share the 5 variable in the Runnbale, as to why the result is not the output of the remaining votes 4 3 2 1 0, this is due to thread scheduling problems, each run will see a different result.
Two ways to create a thread compare thread VS Runnable