In Java, "implements Runnable" and "extends Thread" and implementsrunnable

Source: Internet
Author: User

In Java, "implements Runnable" and "extends Thread" and implementsrunnable

  • Knowledge Point

The differences between "implements Runnable" and "extends Thread"

  • Detailed analysis

When I recently learned about 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) 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?

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 thread

The two methods achieve the same work, but there are 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:

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 for each Thread 36 ExtendsThread 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 enabling the next thread 42 ExtendsThread tc3 = new ExtendsThread (); 43 tc3.start (); 44} 45}

Running result:

We can see from the running results. 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.

Finally, which method is best used?

Obviously, it is better to implement the Runnable interface.


Java extends thread and implements runnable methods are confused. Which of the following examples can be used to explain to me?

Inherit the Thread startup method and directly call the start method of the inherited class
To implement the Runable startup method, new Thread (Object of the implementation class). start () is required ()

Thread problems in java: extends Thread and implements Runnable

Multi-threaded running is achieved by cpu allocation of time slices, and your cycle times are too small! How fast the cpu runs, and the number of cycles is 100, that is not A matter of milliseconds. The cpu just allocated A time slice to thread A, and the task of thread A was completed immediately, you don't have to wait for the next allocation .... You can add a sleep (1000) in the loop to let the thread sleep for one second each time it sells a ticket, and give other threads the running time, or you can view it for tens of thousands of times.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.