The difference between java Thread-Thread and Runnable-threadrunnable
Process: each process has its own code and data space (process context). switching between processes has a large overhead. A process contains 1-n threads.
Thread: the same class of threads share code and data space. Each thread has an independent running stack and program counter (PC), and the thread switching overhead is small.
Threads and processes are divided into five phases: creation, readiness, running, blocking, and termination.
Multi-process means that the operating system can run multiple tasks (programs) at the same time ).
Multithreading means that multiple sequential streams are executed in the same program.
There are two methods to implement multithreading in java: one is to continue the Thread class and the other is to implement the Runable interface.
The Thread class is defined in the java. lang package. As long as a class inherits the Thread class and overwrites the run () method in this class, it can implement multi-threaded operations. However, a class can only inherit one parent class, which is the limitation of this method. I. multi-Thread creation method thread & Runnable simulate train ticket selling method 1 and Thread method
Class MyThread extends Thread {private int ticketsCont = 5; // a total of five train tickets private String name; // window, that is, the Thread name public MyThread (String name) {this. name = name ;}@ Override public void run () {while (ticketsCont> 0) {ticketsCont --; // if there is still a ticket, sell a ticket System. out. println (name + "sold one ticket with the remaining number of votes:" + ticketsCont) ;}} public class TicketsThread {public static void main (String args []) {// create three threads, simulate three window tickets MyThread mt1 = new MyThread ("Window 1"); MyThread mt2 = new MyThread ("window 2 "); myThread mt3 = new MyThread ("window 3"); // start three threads, that is, the window, start to sell tickets mt1.start (); mt2.start (); mt3.start ();}}
In this way, the program can run interactively. So why do we have to use the start (); Method to start multithreading?
In the installation environment of JDK, src.zip is the full java source program. By finding the definition of the start () method in Thread, we can find that private native void start0 () is used in this method (); the native keyword indicates that the underlying functions of the operating system can be called, so this technology becomes JNI technology (java Native Interface ). 2. Runnable Mode
Steps:
1. Define the class to implement the Runnable interface
2. overwrite the run method in the Runnable interface.
Store the code to be run by the thread in the run method.
3. Create a Thread object through the Thread class
4. Pass the subclass object of the Runnable interface as an actual parameter to the constructor of the Thread class.
Why should we pass the subclass object in the Runnable interface to the constructor of Thread?
Because the custom run method belongs to a subclass object of the Runnable interface.
Therefore, to allow the thread to specify the run method of the specified object, it is necessary to specify the object to which the run method belongs.
5. Call the start method of the Thread class to enable the Thread and call the run method of the Runnable interface subclass.
Class MyThread2 implements Runnable {private int ticketsCont = 5; // a total of five train tickets @ Override public void run () {while (true) {synchronized (this) {if (ticketsCont <= 0) {break;} ticketsCont --; // if there is still a ticket, sell the ticket System. out. println (Thread. currentThread (). getName () + "sold one ticket with the remaining number of votes:" + ticketsCont);/* try {Thread. sleep (50); // view the effect through the blocking program} catch (Exception e) {System. out. println (e);} */} public class TicketsRunnable {public static void main (String args []) {MyThread2 mt = new MyThread2 (); // create three threads to simulate the three ticket sales windows Thread th1 = new Thread (mt, "Window 1"); Thread th1 = new Thread (mt, "window 2 "); thread th3 = new Thread (mt, "window 3"); // start three threads, that is, three windows, start to sell tickets th1.start (); th2.start (); th3.start ();}}
However, the start () method is not available in the subclass defined by Runnable, and is available only in the Thread class. Observe the Thread class, there is a constructor: public Thread (Runnable targer) This constructor accepts Runnable subclass instances, that is, you can use the Thread class to start the multi-Thread implemented by Runnable. (Start () can coordinate system resources): II. Summary
- Create: Create a new Thread object, such as Thread thd = new Thread ()
- Ready: After a thread object is created, the start () method of the thread is called. (at this time, the thread knowledge enters the thread queue and waits for the CPU service to be obtained. The running conditions are met, but it may not have started running)
- Run: A ready thread. Once the CPU resource is obtained, it enters the running state and runs the logic in the run () method.
- Termination: the thread's run () method is completed, or the thread calls the stop () method, and the thread enters the termination state.
- Blocking: in some cases, a running thread temporarily gives up CPU resources for some reason, suspends its execution, and enters the blocking state, such as calling sleep () method
In thread-based multithreading (Example 1), three windows are opened, five tickets are sold in each window, and 15 tickets are sold in total, resource Sharing fails, causing program errors.
Based on Runnable, resources are shared using synchronous sharing, and a total of five tickets are sold for the three windows.
We recommend that you use Runnable to create a thread. The same resource in the program refers to the same Runnable object. Synchronized must be added to the secure ticket selling program.
References
1. JAVA multi-Thread VS Runnable
2. Differences between Runnable and Thread in Java