The basic Java tutorial is commonly used to implement multithreading in two ways

  • 2020-04-01 02:44:29
  • OfStack

We'll talk more about thread pools later; Now, take a look at the threads and Runnable. This chapter includes:
A brief introduction to Thread and Runnable
Similarities and differences between Thread and Runnable
Examples of multithreading in Thread and Runnable

Introduction to Thread and Runnable
Runnable is an interface that contains only one run() method. It is defined as follows:


public interface Runnable {
    public abstract void run();
}

Runnable function to implement multithreading. We can define A class A implementation of the Runnable interface; Then, you create A new Thread, such as new Thread(new A()).

Thread is a class. Thread itself implements the Runnable interface. Its statement is as follows:

Public class Thread implements Runnable {}
The role of Thread, multithreading.

Similarities and differences between Thread and Runnable
What Thread and Runnable have in common: both are "multi-threaded implementations."
The differences between Thread and Runnable:
Thread is a class, and Runnable is an interface. Thread itself is the class that implements the Runnable interface. We know that "a class can only have one parent but can implement multiple interfaces," so Runnable is much more extensible.
In addition, Runnable can be used for "resource sharing." That is, multiple threads are built based on a Runnable object, and they share the resources on the Runnable object.
In general, it is recommended to implement multithreading through "Runnable"!

Examples of multithreading in Thread and Runnable
1. Multithreading example of Thread

To better understand Thread and Runnable, take a more persuasive example from an online example.


 //ThreadTest. Java source code
class MyThread extends Thread{  
    private int ticket=10;  
    public void run(){
        for(int i=0;i<20;i++){ 
            if(this.ticket>0){
                System.out.println(this.getName()+"  Tickets: ticket"+this.ticket--);
            }
        }
    } 
};
public class ThreadTest {  
    public static void main(String[] args) {  
        //Start 3 threads t1,t2,t3; Each thread sells 10 tickets!
        MyThread t1=new MyThread();
        MyThread t2=new MyThread();
        MyThread t3=new MyThread();
        t1.start();
        t2.start();
        t3.start();
    }  
}
 

Operation results:


Thread-0  Tickets: ticket10
Thread-1  Tickets: ticket10
Thread-2  Tickets: ticket10
Thread-1  Tickets: ticket9
Thread-0  Tickets: ticket9
Thread-1  Tickets: ticket8
Thread-2  Tickets: ticket9
Thread-1  Tickets: ticket7
Thread-0  Tickets: ticket8
Thread-1  Tickets: ticket6
Thread-2  Tickets: ticket8
Thread-1  Tickets: ticket5
Thread-0  Tickets: ticket7
Thread-1  Tickets: ticket4
Thread-2  Tickets: ticket7
Thread-1  Tickets: ticket3
Thread-0  Tickets: ticket6
Thread-1  Tickets: ticket2
Thread-2  Tickets: ticket6

Results:
(01) MyThread inherits from Thread and is a custom Thread. Each MyThread sells 10 tickets.
(02) the main thread main creates and starts three mythreads. Each of the child threads sold 10 tickets each.

 

2. Example of multithreading in Runnable

Next, we modify the program above. Multithreading is achieved by implementing an interface through Runnable.


//RunnableTest. Java source code
class MyThread implements Runnable{  
    private int ticket=10;  
    public void run(){
        for(int i=0;i<20;i++){ 
            if(this.ticket>0){
                System.out.println(Thread.currentThread().getName()+"  Tickets: ticket"+this.ticket--);
            }
        }
    } 
}; 
public class RunnableTest {  
    public static void main(String[] args) {  
        MyThread mt=new MyThread();
        //Start 3 threads t1,t2, and t3(they share a Runnable object), and these 3 threads sell a total of 10 tickets!
        Thread t1=new Thread(mt);
        Thread t2=new Thread(mt);
        Thread t3=new Thread(mt);
        t1.start();
        t2.start();
        t3.start();
    }  
}

Operation results:


Thread-0  Tickets: ticket10
Thread-2  Tickets: ticket8
Thread-1  Tickets: ticket9
Thread-2  Tickets: ticket6
Thread-0  Tickets: ticket7
Thread-2  Tickets: ticket4
Thread-1  Tickets: ticket5
Thread-2  Tickets: ticket2
Thread-0  Tickets: ticket3
Thread-1  Tickets: ticket1

Results:
(01) is different from "MyThread inherits Thread" above; MyThread here implements the Thread interface.
(02) the main thread main creates and starts three child threads based on "mt this Runnable object". The result is that the three subthreads have sold a total of 10 tickets. This indicates that they share the MyThread interface.


Related articles: