About the thread pool content, we will detail in the future, now, the first thread and runnable to understand. The contents of this chapter include:
Introduction to Thread and runnable
Similarities and differences of thread and runnable
Thread and runnable examples of multithreading
Introduction to Thread and runnable
Runnable is an interface that contains only one run () method. It is defined as follows:
Copy Code code as follows:
Public interface Runnable {
public abstract void run ();
}
Runnable the role of multithreading to achieve. We can define a class A to implement the Runnable interface, and then create a new thread by using new thread (new A ()).
Thread is a class. Thread itself implements the Runnable interface. Its statement is as follows:
public class Thread implements Runnable {}
Thread of the role, to achieve multithreading.
Similarities and differences of thread and runnable
Thread and Runnable the same point: are "multithreading implementation."
Different points of 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 have only one parent class, but it can implement multiple interfaces", so runnable has better extensibility.
In addition, runnable can also be used for "sharing resources." That is, multiple threads are based on a single Runnable object, and they share resources on the Runnable object.
Generally, it is recommended to implement multithreading through "Runnable"!
Thread and runnable examples of multithreading
1. Thread Threading Example
The following example provides a better understanding of thread and runnable, using an example on the Web to compare persuasive examples.
Copy Code code as follows:
Threadtest.java Source
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 () + "Selling Tickets: Ticket" +this.ticket--);
}
}
}
};
public class ThreadTest {
public static void Main (string[] args) {
Start 3 Threads t1,t2,t3 10 tickets per thread!
Mythread t1=new mythread ();
Mythread t2=new mythread ();
Mythread t3=new mythread ();
T1.start ();
T2.start ();
T3.start ();
}
}
Run Result:
Copy Code code as follows:
Thread-0 Selling Tickets: ticket10
Thread-1 Selling Tickets: ticket10
Thread-2 Selling Tickets: ticket10
Thread-1 Selling Tickets: ticket9
Thread-0 Selling Tickets: ticket9
Thread-1 Selling Tickets: ticket8
Thread-2 Selling Tickets: ticket9
Thread-1 Selling Tickets: ticket7
Thread-0 Selling Tickets: ticket8
Thread-1 Selling Tickets: Ticket6
Thread-2 Selling Tickets: ticket8
Thread-1 Selling Tickets: Ticket5
Thread-0 Selling Tickets: ticket7
Thread-1 Selling Tickets: ticket4
Thread-2 Selling Tickets: ticket7
Thread-1 Selling Tickets: Ticket3
Thread-0 Selling Tickets: Ticket6
Thread-1 Selling Tickets: Ticket2
Thread-2 Selling Tickets: Ticket6
Results show:
(mythread) inherits from thread, which is a custom thread. Every mythread will sell 10 tickets.
(02) Main thread main to create and start 3 mythread child threads. Each child thread has sold 10 tickets each.
2. runnable Examples of multithreading
Next, we make changes to the above program. Implement an interface through runnable to achieve multithreading.
Copy Code code as follows:
Runnabletest.java Source
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 () + "Selling Tickets: Ticket" +this.ticket--);
}
}
}
};
public class Runnabletest {
public static void Main (string[] args) {
Mythread mt=new mythread ();
Start 3 thread t1,t2,t3 (they share a runnable object), these 3 threads sell 10 tickets altogether!
Thread T1=new thread (MT);
Thread T2=new thread (MT);
Thread T3=new thread (MT);
T1.start ();
T2.start ();
T3.start ();
}
}
Run Result:
Copy Code code as follows:
Thread-0 Selling Tickets: ticket10
Thread-2 Selling Tickets: ticket8
Thread-1 Selling Tickets: ticket9
Thread-2 Selling Tickets: Ticket6
Thread-0 Selling Tickets: ticket7
Thread-2 Selling Tickets: ticket4
Thread-1 Selling Tickets: Ticket5
Thread-2 Selling Tickets: Ticket2
Thread-0 Selling Tickets: Ticket3
Thread-1 Selling Tickets: Ticket1
Results show:
(01) different from the above "Mythread inherits from Thread"; The Mythread implements the thread interface.
(02) Main thread main to create and start 3 child threads, and these 3 child threads are based on the "Mt this Runnable object" and created. The result was a total of 10 tickets sold for the 3 child threads. This means that they are shared with the Mythread interface.