"Implements Runnable" and "extends Thread" in Java

Source: Internet
Author: User

    • Knowledge points

The difference between "implements Runnable" and "Extends Thread"

    • Specific analysis

When learning about the handler messaging mechanism in Android recently, 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). And what are the similarities and differences between these two ways? With this question, Google has some information out, in the spirit of sharing to everyone at the same time also facilitate their own review, write an article to record it.

First of all, what are these two ways?

1  Public classThreadaImplementsRunnable {2      Public voidrun () {3         //Code4     }5 }6 //call "New Thread (Threada). Start ()" To open the thread7 8  Public classThreadbextendsThread {9      Publicthreadb () {Ten         Super("Threadb"); One     } A      Public voidrun () { -         //Code -     } the } - //call "Threadb.start ()" To open the thread

The same work is done in both ways, but there are some differences between them.

The difference between them is:

1. As we all know, Java is a single inheritance mechanism, and it is not allowed to inherit multiple classes at the same time. Therefore, when you inherit the thread class (extends thread), you can no longer inherit other classes. And you implement the Runnable interface is not the same, you can inherit other classes.

2. When you inherit the thread class, each of your thread objects creates different objects and then associates them.

The inherited runnable interface is not the same, and multiple threads share an object.

Use an example to help us understand:

1 classImplementsrunnableImplementsRunnable {2  3        Private intCounter = 0;4  5        Public voidrun () {6counter++;7System.out.println ("Implementsrunnable:counter:" +counter);8     }9  }Ten   One  classExtendsthreadextendsThread { A   -      Private intCounter = 0; -   the   Public voidrun () { -counter++; -System.out.println ("Extendsthread:counter:" +counter); -  } +  } -   +   Public classthreadvsrunnable { A   at   Public Static voidMain (String args[])throwsException { -  //Multithreading shares an object -implementsrunnable rc =Newimplementsrunnable (); -Thread T1 =NewThread (RC); - T1.start (); -Thread.Sleep (1000);//wait 1 seconds before opening the next thread inThread t2 =NewThread (RC); - T2.start (); toThread.Sleep (1000);//wait 1 seconds before opening the next thread +Thread t3 =NewThread (RC); - T3.start (); the   *  //Create a new instance for each thread $Extendsthread TC1 =NewExtendsthread ();Panax Notoginseng Tc1.start (); -Thread.Sleep (1000);//wait 1 seconds before opening the next thread theExtendsthread TC2 =NewExtendsthread (); + Tc2.start (); AThread.Sleep (1000);//wait 1 seconds before opening the next thread theExtendsthread TC3 =NewExtendsthread (); + Tc3.start (); -  } $}

Operation Result:

From the results of the run we can see. Implements the Runnable interface, creates only one instance of a class, and is shared by multiple threads. So counter increments. Instead of inheriting the thread class, you must create a different instance for each thread. So each instance of the class allocates a different memory space, each with a different counter, with the same values. This means no increase because no reference to an object is the same.

When will I use the Runnable interface?

Use the Runnable interface when you want to access the same resources in a group of threads. In this case, avoid using the thread class, because the creation of multiple objects consumes more memory and can result in large performance costs.

The Runnable interface is implemented inside the Ps:thread class

Finally, which is the best way to use it?

Obviously, the implementation of the Runnable interface is certainly better.

Excerpt from: http://www.cnblogs.com/JohnTsai/p/3979482.html

"Implements Runnable" and "extends Thread" in Java (GO)

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.