标签:
对于 Java 多线程编程中的 implements Runnable 与 extends Thread,部分同学可能会比较疑惑,它们之间究竟有啥区别和联系呢?他们是不是没啥区别随便选呢?实际中究竟该选择哪一个呢?
甚至网上不少博客文章以讹传讹得出不少谬论,那今天的走进科学栏目将带您一一揭开谜底。
其实这块主要是围绕着接口和抽象类的区别以及一些设计原则而言的。
The limitation with "extends Thread" approach is that if you extend Thread, you can not extend anything else . Java does not support multiple inheritance. In reality , you do not need Thread class behavior , because in order to use a thread you need to instantiate one anyway. On the other hand, Implementing the Runnable interface gives you the choice to extend any class you like , but still define behavior that will be run by separate thread.
In "implements Runnable" , we are creating a different Runnable class for a specific behavior job (if the work you want to be done is job). It gives us the freedom to reuse the specific behavior job whenever required. "extends Thread" contains both thread and job specific behavior code. Hence once thread completes execution , it can not be restart again.
Implementing Runnable should be preferred . It does not specializing or modifying the thread behavior . You are giving thread something to run. We conclude that Composition is the better way. Composition means two objects A and B satisfies has-a relationship. "extends Thread" is not a good Object Oriented practice.
"implements Runnable" makes the code loosely-coupled and easier to read . Because the code is split into two classes . Thread class for the thread specific code and your Runnable implementation class for your job that should be run by a thread code. "extends Thread" makes the code tightly coupled . Single class contains the thread code as well as the job that needs to be done by the thread.
"extends Thread" means inheriting all the functions of the Thread class which we may do not need . job can be done easily by Runnable without the Thread class functions overhead.
至此,个人是推荐优先选择 implements Runnable 。
public class Thread extends Object implements Runnable
追踪Thread中的start()方法的定义,可以发现此方法中使用了private native void start0();其中native关键字表示可以调用操作系统的底层函数,这样的技术称为JNI技术(java Native Interface)。
但是在使用Runnable定义的子类中没有start()方法,只有Thread类中才有。此时观察Thread类,有一个构造方法:public Thread(Runnable targer),此构造方法接受Runnable的子类实例,也就是说可以通过Thread类来启动Runnable实现的多线程。
有同学的例子是这样的,参考:http://developer.51cto.com/art/201203/321042.htm:
package tmp; class MyThread extends Thread { private int ticket = 10; private String name; public MyThread(String name) { this.name = name; } public void run() { for (int i = 0; i < 500; i++) { if (this.ticket > 0) { System.out.println(this.name + "卖票---->" + (this.ticket--)); } } } } public class ThreadDemo { public static void main(String[] args) { MyThread mt1 = new MyThread("一号窗口"); MyThread mt2 = new MyThread("二号窗口"); MyThread mt3 = new MyThread("三号窗口"); mt1.start(); mt2.start(); mt3.start(); } } // 一号窗口卖票---->10 // 二号窗口卖票---->10 // 二号窗口卖票---->9 // 二号窗口卖票---->8 // 三号窗口卖票---->10 // 三号窗口卖票---->9 // 三号窗口卖票---->8 ...
Runnable 代码:
package tmp; class MyThread1 implements Runnable { private int ticket = 10; private String name; public void run() { for (int i = 0; i < 500; i++) { if (this.ticket > 0) { System.out.println(Thread.currentThread().getName() + "卖票---->" + (this.ticket--)); } } } } public class RunnableDemo { public static void main(String[] args) { MyThread1 mt = new MyThread1(); Thread t1 = new Thread(mt, "一号窗口"); Thread t2 = new Thread(mt, "二号窗口"); Thread t3 = new Thread(mt, "三号窗口"); t1.start(); t2.start(); t3.start(); } } // 二号窗口卖票---->10 // 三号窗口卖票---->9 // 三号窗口卖票---->7 // 一号窗口卖票---->9 // 三号窗口卖票---->6 // 二号窗口卖票---->8 // 三号窗口卖票---->4 // 一号窗口卖票---->5 // 三号窗口卖票---->2 // 二号窗口卖票---->3 // 一号窗口卖票---->1
由此差别,有同学就得出了一个结论:用Runnable就可以实现资源共享,而 Thread 不可以,这是他们的主要差别之一。。。
其实仔细看看代码就知道,这只是两种写法的区别,根本就不是 implements Runnable 与 extends Thread 的区别:
MyThread1 mt = new MyThread1(); Thread t1 = new Thread(mt,"一号窗口"); Thread t2 = new Thread(mt,"二号窗口"); Thread t3 = new Thread(mt,"三号窗口"); //////////////// Thread t1 = new Thread(new MyThread1(),"一号窗口"); Thread t2 = new Thread(new MyThread1(),"二号窗口"); Thread t3 = new Thread(new MyThread1(),"三号窗口");
其实,想要“资源共享”,Thread 也可以做到的:
private static int ticket = 10; // 三号窗口卖票---->10 // 一号窗口卖票---->9 // 二号窗口卖票---->9 // 一号窗口卖票---->7 // 一号窗口卖票---->5 // 三号窗口卖票---->8 // 一号窗口卖票---->4 // 二号窗口卖票---->6 // 一号窗口卖票---->2 // 三号窗口卖票---->3 // 二号窗口卖票---->1
通过 static 就可以实现拥有共同的ticket=10,但问题也来了,你会发现一二号窗口都卖了第 9 张票。
上面的例子以及结果证明了多线程场景下,需要留意线程安全的问题:
public synchronized void run()
synchronized (Test.class)
private static final Object countLock = new Object(); synchronized (countLock) { count++; }
package tmp; import java.io.IOException; import java.util.concurrent.atomic.AtomicInteger; public class Demo implements Runnable { String name; // static Integer tickets = 20; private static AtomicInteger tickets = new AtomicInteger(20); public Demo(String name) { this.name = name; } public void run() { for (int i = 1; i <= 20; i++) { synchronized (tickets) { if (tickets.get() > 0) { try { Thread.sleep(1000); } catch (InterruptedException e) { } System.out.println("我取票第" + ": " + tickets.getAndDecrement() + " 张票。"); // tickets--; try { Thread.sleep(1000); } catch (InterruptedException e) { } System.out.println("==========现在查询还剩" + ": " + tickets.get() + " 张票。"); } } } } public static void main(String[] args) throws IOException { Demo demo = new Demo("hello"); new Thread(demo).start(); new Thread(demo).start(); new Thread(demo).start(); } }
到这儿,本期走进科学也要跟大家说声再见了,其实聊着聊着感觉都快跑题了,多线程这块话题很多,很复杂,需要慢慢实践与积累,祝大家玩的愉快。
[1] Java线程面试题 Top 50
http://www.importnew.com/12773.html
[2] Difference between “implements Runnable” and “extends Thread” in java
[3] Difference between Thread vs Runnable interface in Java
http://javarevisited.blogspot.com/2012/01/difference-thread-vs-runnable-interface.html
[4] 5 Difference Between "Implements Runnable" And "Extends Thread" In Java
http://javahungry.blogspot.com/2015/05/implements-runnable-vs-extends-thread-in-java-example.html
[5] Java中Runnable和Thread的区别
http://developer.51cto.com/art/201203/321042.htm
[6] Java中继承thread类与实现Runnable接口的区别
http://mars914.iteye.com/blog/1508429
[7] 多线程资源共享的问题
http://blog.csdn.net/king624498030/article/details/45222425
[8] Synchronized和Static Synchronized区别
http://blog.csdn.net/liovey/article/details/7456096
[9] How to synchronize a static variable among threads running different instances of a class in java?
Java 多线程之 Runnable VS Thread 以及资源共享问题
标签:
原文地址:http://my.oschina.net/leejun2005/blog/483999