Questions about the differences between runnable and thread in Java

Source: Internet
Author: User

There are two ways to implement multithreading in Java, one is to inherit the thread class, one to implement the Runnable interface, and the thread class to be defined in the Java.lang package. A class that inherits the thread class and also overwrite the run () in this class

Method enables multithreading, but a class can inherit only one parent class, which is the limit of this method.

In the development of the program as long as the multithreading must always be implemented runnable interface mainly, because the implementation of the Runnable interface compared to inherit the thread class has the following advantages:

1, to avoid the limitation of the point inheritance, a class can inherit multiple interfaces.

2, suitable for the sharing of resources

The above for other blog views, there is also a special article on "runnable can achieve resource sharing but thread cannot realize the reason of resource sharing", this is the link, but if the other way of thinking can also be realized thread resources sharing, I do not know right ...

On the code:

One inherits from the thread class:

Package com.zz.bridge;/** * Extended from Thread class * Copyright June 13, 2015 * created by TXXS * All right reserved */public class Thread Demo0 extends Thread {    private int ticket = 5;            public void Run () {for          (int i =0;i<500;i++) {              if (this.ticket>0) {                  System.out.println ( Thread.CurrentThread (). GetName () + "sell ticket---->" + (this.ticket--));}}}  

An implementation runnable interface

Package com.zz.bridge;/** * Implements runnable interface * Copyright June 13, 2015 * created by TXXS * All right reserved */public class thre AdDemo1 implements Runnable {    private int ticket =5;      @Override public    Void Run () {for          (int i =0;i<500;i++) {              if (this.ticket>0) {                  System.out.println ( Thread.CurrentThread (). GetName () + "sell ticket---->" + (this.ticket--));}}}      

The corresponding test class and the result of the implementation:

Test class for ThreadDemo0:

Package com.zz.bridge;/** * Test inherits from Thread class * Copyright June 14, 2015 * created by TXXS * All right reserved */public class thre adTest2 {public static void main (string[] args) {        /**         * Before the blog (http://blog.chinaunix.net/ uid-20665441-id-310538.html) said,         * Inherit the instance of the thread class, with the same instantiation object will be abnormal, do not understand, personally try         * found both ways can realize thread sharing, do not understand why everyone said not to        */             ThreadDemo0 ticketThread0 = new ThreadDemo0 ();        Thread Th4 = new Thread (ticketThread0);    Thread One        th4.setname ("ticketing port");        Thread th5 = new Thread (ticketThread0);    Thread two        th5.setname ("Ticket Gate One");        Thread th6 = new Thread (ticketThread0);    Line Cheng        th6.setname ("Ticket Gate");        Th4.start ();        Th5.start ();        Th6.start ();}}

ThreadDemo0 results:


Test class for ThreadDemo1:

Package com.zz.bridge;/** * Implements runnable interface * Copyright June 14, 2015 * created by TXXS * All right reserved */public class thre adTest1 {public static void main (string[] args) {ThreadDemo1 ticketThread1 = new ThreadDemo1 ();        Thread Th1 = new Thread (TICKETTHREAD1);    Thread One        th1.setname ("Ticket gate 7");        Thread Th2 = new Thread (TICKETTHREAD1);    Thread two        th2.setname ("Ticket gate 8");        Thread th3 = new Thread (TICKETTHREAD1);    Line Cheng        th3.setname ("Ticket gate 9");        Th1.start ();        Th2.start ();        Th3.start ();}}

Operation Result:


From the above results can be seen, inherited thread and implementation of runnable have realized the sharing of resources, but why do we all think this way is not possible, or I realized the way there is a problem?

Questions about the differences between runnable and thread in Java

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.