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
?