Java Multithreading (1)-thread and runnable

Source: Internet
Author: User

When it comes to Java multithreading, the first thing to think about is thread inheritance and runnable interface implementations

Thread inheritance

public class MyThread extends Thread {public void run () {int i = 0; System.out.println ("--------------" +i++);}}

Runnable Interface Implementation

public class Runnableimpl implements Runnable {Private Long value = 0, @Overridepublic synchronized void run () {while (Threa Dmain.tickets > 0) {System.out.println (Thread.CurrentThread (). GetName () + "------------" +--threadmain.tickets);}}}

Both can be created for multi-threaded threads. In fact, we look at the code implementation of thread, and we can also see that thread actually implements the Runnable interface.

Publicclass Thread implements Runnable {/* Make sure registernatives are the first    thing <clinit> does. */    P rivate static native void Registernatives ();    static {        registernatives ();    }    Private char        name[];    private int priority         ;    Private Thread      THREADQ;    Private long        eetop;

So what's the difference between thread and Runnabe?

The most common difference is

    • When you extends the Thread class, after the can ' t extend any other class which you required. (as you know, Java does isn't allow inheriting more than one class).
    • When you implements Runnable, you can save a space for your class to extend any other class in the future or now.

However, the significant difference is.

    • When you extends the thread class, each of the your thread creates unique object and associate with it.
    • When you implements Runnable, it shares the same object to multiple threads.

Thread vs Runnable

Class Implementsrunnable implements Runnable {private int counter = 0;public void Run () {counter++; System.out.println ("Implementsrunnable:counter:" + Counter);}} Class Extendsthread extends Thread {private int counter = 0;public void Run () {counter++; System.out.println ("Extendsthread:counter:" + Counter);}}  public class Threadvsrunnable {public static void main (String args[]) throws Exception {//multiple threads share the same Object. implementsrunnable rc = new implementsrunnable (); thread T1 = new Thread (RC); T1.start (); Thread.Sleep (1000); Waiting for 1 second before starting next threadthread t2 = new Thread (RC); T2.start (); Thread.Sleep (1000); Waiting for 1 second before starting next threadthread t3 = new Thread (RC); T3.start ();//Creating new instance for ever Y thread access. Extendsthread TC1 = new Extendsthread (); Tc1.start (); Thread.Sleep (1000); Waiting for 1 second before starting next threadextendsthread tc2 = new Extendsthread (); Tc2.start (); Thread.Sleep (1000); WaiTing for 1 second before starting next threadextendsthread TC3 = new Extendsthread (); Tc3.start ();}} 

The execution result output is as follows:

Implementsrunnable:counter:1
Implementsrunnable:counter:2
Implementsrunnable:counter:3
Extendsthread:counter:1
Extendsthread:counter:1
Extendsthread:counter:1

In the Runnable interface approach, only one instance of a class is being created and it have been shared by different thre Ads. The value of counter is incremented for each and every thread access.

Whereas, thread class approach, you must has to create separate instance for every Thread access. Hence different memory is allocated for every class instances and each have separate counter, the value remains SA Me, which means no increment would happen because none of the object reference is same.

Which one is the best for use?

Ans:very simple, based on your application requirements you'll use this appropriately. But I would suggest, try-to-use interface inheritance i.e., implements Runnable.


Java Multithreading (1)-thread and runnable

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.