Thread和Runnable的区别
???????????? 使用多线程时,大家大抵都有过疑惑,到底是继承Thread呢,还是实现Runnable接口,我也有过这样的疑惑,于是便上网搜了下,这边就记下我自己的见解,欢迎指正批评。
????
????????????? 一般使用中,很多有经验的老程序员大部分都是不约而同的使用Runnable多一些,为什么呢,我也问过公司的那些老员工,他们也说不出个所以然,只是使用经验让他们去这样选择,这里我去查看Thread和Runnable的源代码,发觉Thread内部也是实现的Runnable的接口的,慢慢的好像有些头绪,Thread类片段源码如下:
/** * If this thread was constructed using a separate * <code>Runnable</code> run object, then that * <code>Runnable</code> object's <code>run</code> method is called; * otherwise, this method does nothing and returns. * <p> * Subclasses of <code>Thread</code> should override this method. * * @see #start() * @see #stop() * @see #Thread(ThreadGroup, Runnable, String) */ public void run() {if (target != null) { target.run();} }??????????? 这里写到如果target不为空的话,则调用它的run方法,这个target,则是Runnable的实例对象,也就是说,继承于Thread并实现它的run方法能创建一个线程,实际这个run方法就是Runnable所提供的代理对象所有的,因此我们得到Runnable的优点之后,再拿过来跟Thread的优先一一对比,也就大体能知道,使用时,到底选用Thread好还是Runnable好点。
?????????? 经对比,Runnable的优势有以下几点:1.适合多个相同程序代码的线程去处理同一个资源(多线程内的数据共享) 2.避免java特性中的单根继承限制? 3.增加程序健壮性,数据被共享时,仍然可以保持代码和数据的分离和独立? 4.更能体现java面向对象的设计特点
?
?????????? 针对以上的特点2,3,4我们大体可以理解,而对于优势1可能不太深刻,这边,为了体现它在多线程内的数据共享方面的优势,我们用张孝祥所举的经典的买票例子来演示下,多个窗口同时卖票,这里多个窗口即多个线程,所卖的票则是需要共享的数据。
?
?????????? 以下是Thread的实现方式:
class hello extends Thread { public void run() { for (int i = 0; i < 7; i++) { if (count > 0) { System.out.println("count= " + count--); } } } public static void main(String[] args) { hello h1 = new hello(); hello h2 = new hello(); hello h3 = new hello(); h1.start(); h2.start(); h3.start(); } private int count = 5;}?运行结果为:
count= 5count= 4count= 3count= 2count= 1count= 5count= 4count= 3count= 2count= 1count= 5count= 4count= 3count= 2count= 1
????????? 以上结果显示,假使这真是一个买票系统count为总车票数的话,以上代码并未实现线程内的数据共享
?
下面我们看下Runnable的实现,代码如下:
class MyThread implements Runnable{ private int ticket = 5; //5张票 public void run() { for (int i=0; i<=20; i++) { if (this.ticket > 0) { System.out.println(Thread.currentThread().getName()+ "正在卖票"+this.ticket--); } } }}public class lzwCode { public static void main(String [] args) { MyThread my = new MyThread(); new Thread(my, "1号窗口").start(); new Thread(my, "2号窗口").start(); new Thread(my, "3号窗口").start(); }}?运行结果为:
count= 5count= 4count= 3count= 2count= 1
?
Thread类的count是类属性,new hello()调用了3次,3个对象当然是独立的,你把count加上static试试
即时加上static在不同的机器上执行结果肯定不一样的,单核的机器试试看,线程安全要加synchronized或锁的...import java.util.concurrent.TimeUnit;import java.util.concurrent.atomic.AtomicInteger;public class Hello extends Thread { public Hello(String name) {super(name);} public void run() { for (int i = 0; i < 7; i++) { synchronized (count) { if (count.get() > 0) { System.out.println(Thread.currentThread().getName() + " " + "count= " + count.getAndDecrement()); } } try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();} } } public static void main(String[] args) { Hello h1 = new Hello("hello-1"); Hello h2 = new Hello("hello-2"); Hello h3 = new Hello("hello-3"); h1.start(); h2.start(); h3.start(); } private static AtomicInteger count = new AtomicInteger(5); }
http://stackoverflow.com/questions/541487/implements-runnable-vs-extends-thread
有时间也去理一理。。
Thread类的count是类属性,new hello()调用了3次,3个对象当然是独立的,你把count加上static试试
即时加上static在不同的机器上执行结果肯定不一样的,单核的机器试试看,线程安全要加synchronized或锁的...
谢谢指正 8 楼 smilerain 2013-04-26 一个是类对象,一个是接口,如果Thread不适合,就用Runnable接口直接实现,这就是区别 9 楼 点点与麦兜 2013-04-26 这个也能比较吗? 10 楼 nestnall 2013-04-27 接口有一个作用就是用来实现多继承.这个就是区别 11 楼 elgs 2013-04-27 功能上没有任何区别,Thread本身也是实现了Runnable的。 12 楼 oezx 2013-04-27 没看懂啊。没看出什么区别啊。。。。。还不是想怎么用怎么用?? 13 楼 freezingsky 2013-04-27 文档说明中Thread本身就实现了Runnable,所以说比较嘛,其实没有什么可比性了。只是由于java本身的单继承机制,更多让开发者倾向于接口的实现。毕竟通过接口实现,比起通过继承实现,在业务上要方便得多。
