Two ways to create a Thread: Thread VS Runnable and threadrunnable
1. First, there are two ways to create a thread.
One way is to inherit the Thread class and override the run () method.
1 public class MyThread extends Thread {2 @ Override 3 public void run () {4 // TODO Auto-generated method stub 5 6} 7} 8 // The thread uses 9 MyThread mt = new MyThread (); // create thread 10 mt. start (); // start the thread
Another method is to implement the Runnable interface.
1 public class MyThread implements Runnable {2 @ Override 3 public void run () {4 // TODO Auto-generated method stub 5 6} 7} 8 // The Thread uses 9 MyThread mt = new MyThread (); 10 thread Thread = new Thread (mt ); // create thread 11 thread. start (); // start the thread
2. Create thread comparison in two ways
First, we can see from the thread creation method that one inherits one is the implementation interface, but Java can only inherit one parent class and can implement one feature of multiple interfaces, therefore, the Runnable method can avoid the defects caused by Java single inheritance in Thread mode.
Second, Runnable code can be shared by multiple threads (Thread instances), which is suitable for processing unified resources by multiple threads.
Example: Simulate ticket sales. Assume that there are five tickets left, two methods are used to create thread simulation respectively.
TicketThread. java // use inheritance to simulate the sale of five tickets in three windows
1 public class TicketThread {2 public static void main (String [] args) {3 // three threads are created, simulate three windows to sell tickets 4 MyThread mt1 = new MyThread ("thread 1"); 5 MyThread mt2 = new MyThread ("thread 2 "); 6 MyThread mt3 = new MyThread ("thread 3"); 7 8 // start thread 9 mt1.start (); 10 mt2.start (); 11 mt3.start (); 12} 13 14} 15 16 class MyThread extends Thread {17 private int num = 5; // simulation still has 5 tickets 18 private String name; // indicates the thread name 19 20 public MyThread (String name) {21 this. name = name; 22} 23 24 @ Override25 public void run () {26 while (num> 0) {27 num --; 28 System. out. println (name + "sold a ticket with the remaining number of votes" + num); 29} 30} 31}
The running result is:
One thread sold a ticket, the remaining number of votes is 4 2 threads sold a ticket, and the remaining number of votes is 3 threads sold a ticket, the remaining number of votes is 2 4. One thread sold one ticket, the remaining number of votes is 1 5. One thread sold one ticket, and the remaining number of votes is 0 6. Thread 2 sold one ticket, the remaining number of votes is 4, 7, thread 2, 1, and the remaining number of votes is 3, 8, thread 2, and the remaining number of votes is 2, 9, thread 2, and 1, the remaining number of votes is 110, thread 2 sold a ticket, the remaining number of votes is 412, thread 3 sold a ticket, and the remaining number of votes is, thread 3 sold a ticket, the remaining number of votes is 313. Thread 3 sold one ticket, the remaining number of votes is 214. Thread 3 sold one ticket, the remaining number of votes is 115. Thread 3 sold one ticket, and the remaining number of votes is 0.
We can see that a total of 15 tickets have been sold, and each thread has 5 tickets.
TicketRunnable. java // use Runnable
1 public class TickerRunnable {2 public static void main (String [] args) {3 MyThread mt = new MyThread (); 4 // create three threads 5 Thread t1 = new Thread (mt, "Thread 1"); 6 Thread t2 = new Thread (mt, "Thread 2 "); 7 Thread t3 = new Thread (mt, "Thread 3"); 8 // start Thread 9 t1.start (); 10 t2.start (); 11 t3.start (); 12} 13 14} 15 16 class MyThread implements Runnable {17 private int num = 5; // simulation of the remaining 5 tickets 18 19 @ Override20 public void run () {21 while (num> 0) {22 num --; 23 System. out. println (Thread. currentThread (). getName () + "sold a ticket with the remaining number of votes" + num); 24} 25} 26}
The running result is:
One thread sold one ticket, the remaining number of votes was 42, thread 3 sold one ticket, and the remaining number of votes was 33, thread 3 sold one ticket, the remaining number of votes is 14, thread 2 sold a ticket, the remaining number of votes is 05, thread 1 sold a ticket, and the remaining number of votes is 2
It can be seen that multiple threads share the variable 5 in Runnbale. as to why the result is not output with the remaining number of votes 4 3 2 1 0, this is caused by thread scheduling problems, different results are displayed each time you run the command.