1. There are two ways to create thread threads in Java:
(1) By inheriting the thread class and overriding the thread's run () method, the logic in which the threads run is placed.
(2) Instantiate the thread class by implementing the Runnable interface.
2. In practical applications, 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.
1 Packagecom.threadtest;2 classMyThreadextendsthread{3 4 Private intTicket = 10;5 PrivateString name;6 PublicMyThread (String name) {7 This. Name =name;8 }9 Ten Public voidrun () { One for(intI =0;i<500;i++){ A if( This. ticket>0){ -System.out.println ( This. name+ "Sell ticket---->" + ( This. ticket--)); - } the } - } - } - Public classThreaddemo { + - + Public Static voidMain (string[] args) { AMyThread mt1=NewMyThread ("window number One")); atMyThread mt2=NewMyThread ("Window No. Second"); -MyThread mt3=NewMyThread ("Window No. Third"); - Mt1.start (); - Mt2.start (); - Mt3.start (); - } in -}
The results of the operation are as follows:
1A window sells tickets---->102A window sells tickets---->93Window second sell ticket---->104A window sells tickets---->85A window sells tickets---->76A window sells tickets---->67Window third sell ticket---->108A window sells tickets---->59A window sells tickets---->4TenA window sells tickets---->3 OneA window sells tickets---->2 AA window sells tickets---->1 -Window second sell ticket---->9 -Window second sell ticket---->8 theWindow third sell ticket---->9 -Window third sell ticket---->8 -Window third sell ticket---->7 -Window third sell ticket---->6 +Window third sell ticket---->5 -Window third sell ticket---->4 +Window third sell ticket---->3 AWindow third sell ticket---->2 atWindow third sell ticket---->1 -Window second sell ticket---->7 -Window second sell ticket---->6 -Window second sell ticket---->5 -Window second sell ticket---->4 -Window second sell ticket---->3 inWindow second sell ticket---->2 -Window second sell ticket---->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:
1 window sell ticket---->10 2 Third window sell ticket---->9 3 Third window sell ticket---->7 4 Third window sell ticket---->5 5 Third window sell ticket---->4 6 third window sell ticket---->3 7 Third window sell ticket---->2 8 Third window sell ticket---->1 9 A window sells tickets---->8 second window---->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 at the same time as the creation of 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 that multiple threads work 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.
Java Basics Hardening Multi-Threading Note 05:java The difference between the thread class and the implementation of the Runnable interface