Question about the difference between Runnable and Thread in Java

Source: Internet
Author: User

Question about the difference between Runnable and Thread in Java

In java, You can implement multithreading in two ways. One is to inherit the Thread class, the other is to implement the Runnable interface, and the Thread class is defined in the java. lang package. As long as a class inherits the Thread class and overwrites the run ()

Methods can implement multi-threaded operations, but a class can only inherit one parent class, which is a limitation of this method.

In program development, as long as it is multi-Thread, it will always be dominated by implementing the Runnable interface, because implementing the Runnable interface has the following advantages over inheriting the Thread class:

1. Avoid the limitations of point inheritance. A class can inherit multiple interfaces.

2. Suitable for Resource Sharing

The above is my opinion on other blogs. There is also a special article titled "Why Runnable can share resources but Thread cannot share resources". This is a link, however, if another idea is used, it seems that Thread resources can be shared...

Code:

A class inherited from Thread:

 

Package com. zz. bridge;/*** extended self-Thread class * Copyright June 13, 2015 * created by txxs * all right reserved */public class ThreadDemo0 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 tickets ---->" + (this. ticket --));}}}}

A Runnable interface

 

 

Package com. zz. bridge;/*** implement the Runnable interface * Copyright June 13, 2015 * created by txxs * all right reserved */public class ThreadDemo1 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 tickets ---->" + (this. ticket --));}}}}

Corresponding test classes and implementation results:

 

ThreadDemo0 test class:

 

Package com. zz. bridge;/*** test inherited from Thread class * Copyright June 14, 2015 * created by txxs * all right reserved */public class ThreadTest2 {public static void main (String [] args) {/*** previous blog (http://blog.chinaunix.net/uid-20665441-id-310538.html) said, * inheriting the instance of the Thread class, with the same instantiated object will be abnormal, do not understand, I tried it myself * and found that both methods can achieve thread sharing. I don't understand why everyone said no */ThreadDemo0 ticketThread0 = new ThreadDemo0 (); thread th4 = new Thread (ticketThread0); // Thread 1 th4.setName ("Ticket Gate 10"); Thread th5 = new Thread (ticketThread0 ); // Thread 2 th5.setName ("Ticketing port 11"); Thread th6 = new Thread (ticketThread0); // Thread 3 th6.setName ("Ticketing Port 12"); th4.start (); th5.start (); th6.start ();}}

ThreadDemo0 result:

 

ThreadDemo1 test class:

 

Package com. zz. bridge;/*** Runnable interface * Copyright June 14, 2015 * created by txxs * all right reserved */public class ThreadTest1 {public static void main (String [] args) {ThreadDemo1 ticketThread1 = new ThreadDemo1 (); Thread th1 = new Thread (ticketThread1); // Thread 1 th1.setName ("Ticket Gate 7"); Thread YY = new Thread (ticketThread1 ); // Thread 2 th2.setName ("Ticket Gate 8"); Thread th3 = new Thread (ticketThread1); // Thread 3 th3.setName ("Ticket Gate 9"); th1.start (); th2.start (); th3.start ();}}

Running result:

 

From the above running results, we can see that both Thread inheritance and Runnable implementation achieve resource sharing. But why do you think this method is not feasible? Is there a problem with my implementation method?

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.