In this chapter, we learn "2 common ways to implement multithreading": Thread and Runnable.
It is commonly used because multithreading can also be achieved by java.util.concurrent the thread pool in the package. 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
Reprint please indicate the source: http://www.cnblogs.com/skywang12345/p/3479063.html
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 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.
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 () + "Sell Tickets: Ticket" +this.ticket--);
}}}
;
public class ThreadTest {public
static void Main (string[] args) {
//starts 3 threads t1,t2,t3; Each thread sells 10 tickets each!
mythread t1=new mythread ();
Mythread t2=new mythread ();
Mythread t3=new mythread ();
T1.start ();
T2.start ();
T3.start ();
}
Run Result:
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
Thread-2 Selling Tickets: Ticket5
Thread-2 Selling Tickets: Ticket4 Thread-1 selling Tickets: Ticket1 Thread-0
Selling Tickets:
ticket5 Thread-2 Selling tickets: Ticket3 Thread-0 selling Tickets: Ticket4 Thread-2 selling Tickets: Ticket2 Thread-0 selling Tickets: Ticket3 Thread-2
selling Tickets: Ticket1
Thread-0 Selling Tickets: Ticket2
Thread-0 Selling Tickets: Ticket1