We will introduce the content of thread pool in detail later; now, let’s first understand Thread and Runnable. The content of this chapter includes:
Introduction to Thread and Runnable
Similarities and differences between Thread and Runnable
Example of multithreading for Thread and Runnable
Introduction to Thread and Runnable
Runnable is an interface that only contains one run() method. Its definition is as follows:
The code copy is as follows:
public interface Runnable {
public abstract void run();
}
The role of Runnable, implements multi-threading. We can define a class A to implement the Runnable interface; then create a new thread through new Thread(new A()) and other methods.
Thread is a class. Thread itself implements the Runnable interface. Its statement is as follows:
public class Thread implements Runnable {}
The role of Thread is to realize multi-threading.
Similarities and differences between Thread and Runnable
The similarity between Thread and Runnable: both are “multi-threaded implementations.”
The differences between Thread and Runnable:
Thread is a class, and Runnable is an interface; Thread itself is a class that implements the Runnable interface. We know that "a class can only have one parent class, but it can implement multiple interfaces", so Runnable has better scalability.
In addition, Runnable can also be used for "sharing of resources". That is, multiple threads are created based on a certain Runnable object, and they will share resources on the Runnable object.
Generally, it is recommended to implement multi-threading through "Runnable"!
Multithreading examples for Thread and Runnable
1. Multithreading example of Thread
Below we can better understand Thread and Runnable through examples, and draw on an example online that is more convincing.
The code copy is as follows:
// 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()+" Ticket Sell: 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();
}
}
Running results:
The code copy is as follows:
Thread-0 Ticket Selling: ticket10
Thread-1 Ticket Selling: ticket10
Thread-2 Ticket Selling: ticket10
Thread-1 Ticket Selling: ticket9
Thread-0 Ticket Selling: ticket9
Thread-1 Ticket Selling: ticket8
Thread-2 Ticket Selling: ticket9
Thread-1 Ticket Selling: ticket7
Thread-0 Ticket Selling: ticket8
Thread-1 Ticket Selling: ticket6
Thread-2 Ticket Selling: ticket8
Thread-1 Ticket Selling: ticket5
Thread-0 Ticket Selling: ticket7
Thread-1 Tickets: ticket4
Thread-2 Ticket Selling: ticket7
Thread-1 Ticket Selling: ticket3
Thread-0 Ticket Selling: ticket6
Thread-1 Ticket Selling: ticket2
Thread-2 Ticket Selling: ticket6
Results description:
(01) MyThread inherits from Thread, which is a custom thread. Each MyThread sells 10 tickets.
(02) The main thread main creates and starts 3 MyThread child threads. Each child thread sells 10 tickets.
2. Multithreaded example of Runnable
Next, we modify the above program. Implement an interface through Runnable, thereby implementing multi-threading.
The code copy is as follows:
// 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()+" Ticket Sell: ticket"+this.ticket--);
}
}
}
};
public class RunnableTest {
public static void main(String[] args) {
MyThread mt=new MyThread();
// Start 3 threads t1, t2, 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();
}
}
Running results:
The code copy is as follows:
Thread-0 Ticket Selling: ticket10
Thread-2 Ticket Selling: ticket8
Thread-1 Ticket Selling: ticket9
Thread-2 Ticket Selling: ticket6
Thread-0 Ticket Selling: ticket7
Thread-2 Tickets: ticket4
Thread-1 Ticket Selling: ticket5
Thread-2 Tickets: ticket2
Thread-0 Ticket Selling: ticket3
Thread-1 Tickets: ticket1
Results description:
(01) Unlike the above "MyThread inherits from Thread"; here MyThread implements the Thread interface.
(02) The main thread main creates and starts 3 child threads, and these 3 child threads are all created based on "mt, Runnable object". The result of the operation is that these 3 child threads sold a total of 10 tickets. This means that they share the MyThread interface.