I. Problem INTRODUCTION
Speaking of these two methods have to say multi-threading, talking about multithreading will have to implement multithreading two ways to inherit the thread class and implement the Runable interface, the following first look at the difference between the two ways.
Two. Two ways to implement multithreading in Java
1. Inheriting the thread class
/*** Use the thread class to simulate 4 ticket windows together to sell 100 train ticket procedures, in fact, each sell 100*/ Public classThreadTest { Public Static voidMain (string[] args) {NewMyThread (). Start (); NewMyThread (). Start (); NewMyThread (). Start (); NewMyThread (). Start (); } //You can also write the class outside . Public Static classMyThreadextendsthread{Private inttickets=100; Public voidrun () { while(tickets>0) {System.out.println ( This. GetName () + "sell First" "+tickets--+" "Train Ticket"); } } }}
2. Implement the Runable interface
/*** Use runnable interface to simulate 4 ticket windows to sell 100 train tickets together*/ Public classRunnabletest { Public Static voidMain (string[] args) {Runnable Runnable=NewMyThread (); NewThread (runnable). Start (); NewThread (runnable). Start (); NewThread (runnable). Start (); NewThread (runnable). Start (); } Public Static classMyThreadImplementsrunnable{Private inttickets=100; Public voidrun () { while(tickets>0) {System.out.println (Thread.CurrentThread (). GetName ()+ "Sell First" "+tickets--+" "Train Ticket"); } } }}
In either case, the Run method can be overridden with an inner class or an external class, although the inner class is generally used.
3. Comparison of two different ways
In practice, the implementation of Runable interface is often used, on the one hand, because Java only supports single inheritance, inheriting the thread class can no longer inherit other classes, and the Runable interface has only one run method, on the other hand, through the results can be seen to implement Runable interface is the real multi-threading ... ...
Three. The difference between two different methods
1) Start:
Start the thread with the Start method, and actually implement multi-threaded operation, without waiting for the Run method body code to complete and proceed directly to execute the following code. By invoking the start () method of the thread class to start a thread, the thread is in a ready (operational) state and is not running, and once the CPU time slice is taken, the run () method is started, where the method run () is called the thread body, which contains the contents of the thread to be executed. This thread terminates when the Run method finishes running.
2) Run:
The run () method is just a common method of the class, if you call the Run method directly, the program is still only the main thread of the threads, its program execution path is only one, or to execute sequentially, or to wait for the Run method body execution before you can continue to execute the following code, This will not achieve the purpose of writing threads. Summary: Call the Start method to start the thread, and the Run method is just a normal method call to thread, or execute in the main thread. Both methods should be familiar, put code that needs to be processed in parallel in the run () method, and the start () method will automatically call the run () method, which is prescribed by the JVM's memory mechanism. and the run () method must be public access, and the return value type is void: