There are two ways to implement multithreaded code in Java, one is to inherit the thread class , and the other is to implement the Runnable interface .
First, inherit the Thread class
The thread class is defined in the Java.lang package, and a class is referred to as a multithreaded operation class as long as it inherits the thread class.
In the thread subclass, you must explicitly overwrite the run () method in the thread class, which is the principal
Example: Inheriting the thread class for multithreading
Class MyThread extends Thread {//Inherit the thread class PrivateString name; Public MyThread(String name) { This. name = name; } Public void Run() {//Overwrite the Run method in the thread class for(intI=0;i<5; i++) {System.out.println (name +"Run: i ="+ i); } }} Public class threaddemo { Public Static void Main(string[] args) {MyThread MT1 =NewMyThread ("Thread A"); MyThread mt2 =NewMyThread ("Thread B"); Mt1.run (); Mt2.run (); }}
Program Run Results
线程A 运行:i0线程A 运行:i1线程A 运行:i2线程A 运行:i3线程A 运行:i4线程B 运行:i0线程B 运行:i1线程B 运行:i2线程B 运行:i3线程B 运行:i4
It is found that the above program executes the MT1 object before executing the Mt2 object, and does not run interleaved, that is, the thread is not actually started at this time,
is still a sequential execution, how do you start a thread? If you want to start the thread correctly, you cannot call the run () method directly, but you should
is to invoke the start () method inherited from the Thread class, with the following code:
Example: Starting a thread
Class MyThread extends Thread {//Inherit the thread class PrivateString name; Public MyThread(String name) { This. name = name; } Public void Run() {//Overwrite the Run method in the thread class for(intI=0;i<5; i++) {System.out.println (name +"Run: i ="+ i); } }} Public class threaddemo { Public Static void Main(string[] args) {MyThread MT1 =NewMyThread ("Thread A"); MyThread mt2 =NewMyThread ("Thread B"); Mt1.start ();//Start multithreadingMt2.start (); }}
Program run results (one possible result)
线程A 运行:i0线程B 运行:i0线程A 运行:i1线程B 运行:i1线程A 运行:i2线程B 运行:i2线程A 运行:i3线程B 运行:i3线程A 运行:i4线程B 运行:i4
From the running results of the program, it can be found that two threads are now interleaved, which thread object has grabbed the CPU resources, which thread can run,
So the program each time the result is not the same, while the thread starts the call is the start () method, but actually called the run () method of the body
Second, realize Runnable interface
Multi-threading can also be implemented in Java by implementing the Runnable interface, where only an abstract method is defined in the Runnable interface: public void Run ();
Example: Implementing the Runnable Interface
class MyThread implements Runnable { //实现Runnable接口 private String name; publicMyThread(String name) { this.name = name; } publicvoidrun() { //覆写Runnable类中的run 方法 for(int i=0;i<5;i++){ " 运行:i = " + i); } }}
The above code through the implementation of the Runnable interface implementation of multi-threading, from the previous code can be known, to start a multi-threaded must use the start () method to complete
If you inherit the thread class, you can use the start () method directly from the thread class but now implement the Runnable interface, how do you start multithreading?
In fact, this is still dependent on the thread class to complete the boot, and the thread class provides a
Public thread (Runnable target) and public thread (Runnable target,string name) two constructor methods
Both of these construction methods can receive the subclass instance object of Runnable, so you can rely on this point to start multithreading
Example: Using the thread class to start multithreading
Class MyThread implements Runnable {//Implement Runnable interface PrivateString name; Public MyThread(String name) { This. name = name; } Public void Run() {//Overwrite the Run method in the Runnable class for(intI=0;i<5; i++) {System.out.println (name +"Run: i ="+ i); } }} Public class threaddemo { Public Static void Main(string[] args) {MyThread MT1 =NewMyThread ("Thread A");//instantiation of Runnable subclass ObjectMyThread mt2 =NewMyThread ("Thread B"); Thread T1 =NewThread (MT1);//Instantiate the thread class objectThread t2 =NewThread (MT2); T1.start ();//Start threadT2.start (); }}
Program Run Results
线程B 运行:i0线程A 运行:i0线程B 运行:i1线程A 运行:i1线程B 运行:i2线程B 运行:i3线程A 运行:i2线程B 运行:i4线程A 运行:i3线程A 运行:i4
From the above two implementations can be found, regardless of which way, eventually must rely on the thread class to start multithreading
Third, thread class and Runnable interface
Multi-threading can be implemented through the thread class and the Runnable interface, what are the links and differences between the two, and the following is a look at the definition of the thread class
public class Thread extends Object implements Runnable
From the definition of the thread class, it can be found that the thread class is also a subclass of the Runnable interface. In fact, between the thread class and the Runnable interface, it is also
There is a difference, if a class inherits the thread class, it is not suitable for multiple threads to share resources, and implements the Runnable interface, it is easy to realize the sharing of resources.
Example: Inheriting the thread class cannot share resources
Class MyThread extends Thread {//Inherit the thread class Private intTicket =5;//Altogether 5 tickets Public void Run() {//Overwrite the Run method for(inti =0; I < -; i++) {if(Ticket >0){//Determine if there are any remaining ticketsSystem.out.println ("Sell ticket: Ticket ="+ ticket--); } } }} Public class threaddemo { Public Static void Main(string[] args) {MyThread MT1 =NewMyThread (); MyThread mt2 =NewMyThread (); MyThread MT3 =NewMyThread (); Mt1.start ();//Start threadMt2.start (); Mt3.start (); }}
Program Run Results
卖票:ticket = 5卖票:ticket = 5卖票:ticket = 5卖票:ticket = 4卖票:ticket = 4卖票:ticket = 3卖票:ticket = 4卖票:ticket = 2卖票:ticket = 3卖票:ticket = 1卖票:ticket = 3卖票:ticket = 2卖票:ticket = 2卖票:ticket = 1卖票:ticket = 1
The above program through the thread class to implement multi-threading, the program started three threads, but three threads sold each of their 5 tickets,
did not achieve the purpose of resource sharing.
Example: Implementing the Runnable interface to achieve resource sharing
Class MyThread implements Runnable {//Implement Runnable interface Private intTicket =5;//Altogether 5 tickets Public void Run() {//Overwrite the Run method for(inti =0; I < -; i++) {if(Ticket >0){//Determine if there are any remaining ticketsSystem.out.println ("Sell ticket: Ticket ="+ ticket--); } } }} Public class threaddemo { Public Static void Main(string[] args) {MyThread MT =NewMyThread ();NewThread (MT). Start ();//Start thread NewThread (MT). Start ();NewThread (MT). Start (); }}
Program Run Results
卖票:ticket = 5卖票:ticket = 3卖票:ticket = 4卖票:ticket = 1卖票:ticket = 2
From the running results of the program, it can be found that although 3 threads were started, 3 threads sold only 5 tickets, that is, the ticket property is shared by all thread objects
It can be seen that implementing the Runnable interface has the following significant advantages over inheriting the thread class:
A scenario that is appropriate for multiple threads of the same program code to handle the same resource.
Avoid limitations due to the single inheritance of Java
The robustness of the program is enhanced, the code can be shared by multiple threads, and the code is independent of the data.
Therefore, it is recommended to use the Runnable interface to implement multithreading in development
Itmyhome
Implementation of thread threads in Java