1. By inheriting the thread class, overriding the thread's run () method, placing the logic in which the threads run
2. Instantiate the thread class by implementing the Runnable interface
In the practical application, we often use multi-threading, such as the station ticketing system, the station's various ticket outlets equal to each thread. When we do this system may think of two ways to achieve, inherit the thread class or implement the Runnable interface, now look at the two ways to achieve the results.
Packagecom.threadtest; classMyThreadextendsthread{Private intTicket = 10; PrivateString name; PublicMyThread (String name) { This. Name =name; } Public voidrun () { for(intI =0;i<500;i++){ if( This. ticket>0) {System.out.println ( This. name+ "Sell ticket---->" + ( This. ticket--)); } } } } Public classThreaddemo { Public Static voidMain (string[] args) {MyThread mt1=NewMyThread ("window number One")); MyThread mt2=NewMyThread ("Window No. Second"); MyThread Mt3=NewMyThread ("Window No. Third"); Mt1.start (); Mt2.start (); Mt3.start (); } }
The results of the operation are as follows:
A window sells tickets---->10a window to sell tickets---->9window second, sell tickets .---->10a window to sell tickets---->8a window to sell tickets---->7a window to sell tickets---->6window third, sell tickets .---->10a window to sell tickets---->5a window to sell tickets---->4a window to sell tickets---->3a window to sell tickets---->2a window to sell tickets---->1window second, sell tickets .---->9window second, sell tickets .---->8window third, sell tickets .---->9window third, sell tickets .---->8window third, sell tickets .---->7window third, sell tickets .---->6window third, sell tickets .---->5window third, sell tickets .---->4window third, sell tickets .---->3window third, sell tickets .---->2window third, sell tickets .---->1window second, sell tickets .---->7window second, sell tickets .---->6window second, sell tickets .---->5window second, sell tickets .---->4window second, sell tickets .---->3window second, sell tickets .---->2window second, sell tickets .---->1
The code for implementing the Runnable interface is as follows:
Packagecom.threadtest; classMyThread1Implementsrunnable{Private intTicket =10; PrivateString name; Public voidrun () { for(intI =0;i<500;i++){ if( This. ticket>0) {System.out.println (Thread.CurrentThread (). GetName ()+ "Sell ticket---->" + ( This. ticket--)); } } } } Public classRunnabledemo { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub//design of three threadsMyThread1 MT =NewMyThread1 (); Thread T1=NewThread (MT, "window number One")); Thread T2=NewThread (MT, "Window No. Second"); Thread T3=NewThread (MT, "Window No. third"); //MyThread1 mt2 = new MyThread1 (); //MyThread1 mt3 = new MyThread1 (); T1.start (); T2.start (); T3.start (); } }
The results of the operation are as follows:
One window selling ticket---->10 Third window sell ticket---->9 Third window sell ticket---->7 Third window sell ticket---->5 Third window sell ticket---->4 Third window sell ticket---->3 Third window sell ticket---->2 Third window sell ticket ---->1 window sells tickets---->8 second ---->6
Why this kind of result is present. We might as well make a metaphor, actually just the procedure,
Inherit the thread class, we are equivalent to take out three things namely three tickets to sell 10 of the task divided to three windows, they each do the various things sold each of the tickets to complete each task, because Mythread inherits the thread class, so in new Mythread creates three threads while creating three objects;
Achieve runnable, equivalent to take out a ticket to sell 10 of the task to three people to work together, new mythread equivalent to create a task, and then instantiate three thread, create three threads to arrange three windows to execute.
The diagram shows the following:
We may be confused when we touch. Inherit the thread class and implement the Runnable interface to implement multi-threading, in fact, after contact we will find that this is completely two different implementations of multi-threading, one is multiple threads to complete their own tasks, one is multiple threads together to complete a task.
In fact, in the implementation of a task with multiple threads to do can also be used to inherit the thread class to achieve is just more trouble, generally we use the implementation of runnable interface to achieve, concise and clear.
In most cases, you should use the Runnable interface if you only want to override the run () method without overriding the other Thread methods. This is important because a subclass should not be created for the class (Thread) unless the programmer intends to modify or enhance the basic behavior of the class.
The difference between inheriting the thread class and implementing the Runnable interface in Java