"Implements Runnable" and "extends Thread" in Java"
Different analysis of knowledge points "implements Runnable" and "extends Thread": When I recently learned the Handler message transmission mechanism in Android, there are two ways to create a new Thread: one is to implement the Runnable interface (implements Runnable), and the other is to inherit the Thread class (extends Thread ). What are the similarities and differences between the two methods? With this question, Google posted some documents and shared them with you. At the same time, it is convenient for you to read and review and write an article to record them. First, let's take a look at the two methods? Copy Code 1 public class ThreadA implements Runnable {2 public void run () {3 // Code 4} 5} 6 // call "new Thread (threadA ). start () "to enable Thread 7 8 public class ThreadB extends Thread {9 public ThreadB () {10 super (" ThreadB "); 11} 12 public void run () {13 // Code14} 15} 16 // call "threadB. start () "to enable the two methods of thread replication code to achieve the same work, but there are still some differences between them. The differences between them are: 1. We all know that Java is a single Inheritance Mechanism and cannot inherit multiple classes at the same time. Therefore, after you inherit the Thread class (extends Thread), you cannot inherit other classes. The Runnable interface is different. You can inherit other classes. 2. When you inherit the Thread class, each of your Thread objects creates different objects and associates them. The inherited Runnable interface is different. multiple threads share an object. Use an example to help us understand: Copy code 1 class ImplementsRunnable implements Runnable {2 3 private int counter = 0; 4 5 public void run () {6 counter ++; 7 System. out. println ("ImplementsRunnable: Counter:" + counter); 8} 9} 10 11 class ExtendsThread extends Thread {12 13 private int counter = 0; 14 15 public void run () {16 counter ++; 17 System. out. println ("ExtendsThread: Counter:" + counter); 18} 19} 20 21 public Class ThreadVsRunnable {22 23 public static void main (String args []) throws Exception {24 // multiple threads share an object 25 ImplementsRunnable rc = new ImplementsRunnable (); 26 Thread t1 = new Thread (rc); 27 t1.start (); 28 Thread. sleep (1000); // wait for 1 second before starting the next Thread 29 Thread t2 = new Thread (rc); 30 t2.start (); 31 Thread. sleep (1000); // wait for 1 second before starting the next Thread 32 Thread t3 = new Thread (rc); 33 t3.start (); 34 35 // create a new instance 36 ExtendsThread for each thread Tc1 = new ExtendsThread (); 37 tc1.start (); 38 Thread. sleep (1000); // wait for 1 second before enabling the next Thread 39 ExtendsThread tc2 = new ExtendsThread (); 40 tc2.start (); 41 Thread. sleep (1000); // wait 1 second before starting the next thread 42 ExtendsThread tc3 = new ExtendsThread (); 43 tc3.start (); 44} 45} from the running result, we can see that. To implement the Runnable interface, only one class instance is created and shared by multiple threads. Therefore, Counter increases progressively. To inherit the Thread class, you must create different instances for each Thread. Therefore, instances of each class are allocated with different Memory Spaces. Each class has different counters and their values are the same. This means there is no addition because no object reference is the same. When will the Runnable interface be used? Use the Runnable interface when you want to access the same resources in a set of threads. In this case, avoid using the Thread class, because the creation of multiple objects will occupy more memory, resulting in high performance costs. PS: the Runnable interface is implemented in the Thread class. which method is the best? Obviously, it is better to implement the Runnable interface.