Runnable和Thread比较

看代码

public static void main(String[] args) {  
        // TODO Auto-generated method stub  
              
            new MyThread().start();  
            new MyThread().start();  
              
    }  
  
  
class MyThread extends Thread{    
    private int ticket = 5;    
    public void run(){    
         while(true){  
             System.out.println("Thread ticket = " + ticket--);    
             if(ticket < 0){    
                break;  
             }    
         }    
    }    
}   

输出结果:

Thread ticket = 5  
Thread ticket = 5  
Thread ticket = 4  
Thread ticket = 4  
Thread ticket = 3  
Thread ticket = 2  
Thread ticket = 3  
Thread ticket = 1  
Thread ticket = 2  
Thread ticket = 0  
Thread ticket = 1  
Thread ticket = 0  

这样多卖了一半的票显然不合理,用runnable试试:

public static void main(String[] args) {  
        // TODO Auto-generated method stub  
        MyThread2 m=new MyThread2();  
            new Thread(m).start();  
            new Thread(m).start();  
          
              
    }  
class MyThread2 implements Runnable{    
    private int ticket = 5;    
    public void run(){    
         while(true){  
             System.out.println("Runnable ticket = " + ticket--);    
             if(ticket < 0){    
                break;  
             }    
         }   
    }    
}    

输出结果:

Runnable ticket = 5  
Runnable ticket = 4  
Runnable ticket = 3  
Runnable ticket = 2  
Runnable ticket = 1  
Runnable ticket = 0  

这样的结果才合理。
很明显这个例子完全错误,多卖票的原因根本不是因为Runnable和Thread的区别,看调用就知道了。
使用Thread的时候是这样调用的:

new MyThread().start();  
new MyThread().start(); 

而使用Runnable的时候是这样调用的:

new Thread(m).start();  
new Thread(m).start();  

新建了两个MyThread对象去卖票,不卖两倍票才怪呢,而Runnable卖票的时候是同一个Runnable对象,肯定只卖一倍票,所以这个例子根本没体现Runnable和Thread的区别,再来看一个例子:

public class TicketThread extends Thread{

    private int ticket = 10;

    public void run(){
        for(int i =0;i<10;i++){
            synchronized (this){
                if(this.ticket>0){
                    try {
                        Thread.sleep(100);
                        System.out.println(Thread.currentThread().getName()+"卖票---->"+(this.ticket--));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public static void main(String[] arg){
        TicketThread t1 = new TicketThread();
        new Thread(t1,"线程1").start();
        new Thread(t1,"线程2").start();
    }
}

输出结果:

线程1卖票—->10 
线程1卖票—->9 
线程1卖票—->8 
线程2卖票—->7 
线程2卖票—->6 
线程1卖票—->5 
线程1卖票—->4 
线程2卖票—->3 
线程2卖票—->2 
线程1卖票—->1

(这里必须使用synchronized,否则会出现重复卖某一张票的情况,当然这点和本篇文章无关,这里不做详述。)
这样就达到了卖一倍票的结果,没毛病。这样看起来,Thread和Runnable岂不是没区别了?
找到答案很简单,点进去看Thread源码就知道了

public class Thread implements Runnable {}

可以看出,Thread实现了Runnable接口,这和上面例子中的MyThread2一样了,当我们使用

TicketThread t1 = new TicketThread();
new Thread(t1,"线程1").start();

这种写法时,会被认为智障。。。。没错,就是智障,这是脱裤子放屁的写法,标准写法应该是:

TicketThread t1 = new TicketThread();
t1.start();

看看源码:

    public Thread(Runnable target) {
        init(null, target, "Thread-" + nextThreadNum(), 0);
    }

new Thread( new TicketThread(),"线程1").start();这种写法里面,TicketThread会被当做一个Runnable,那我还要写一个TicketThread类干嘛?所以这就是脱裤子放屁。
说了这么多,那两者的区别到底是什么?
来看看Runnable的代码:

public interface Runnable {
    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see     java.lang.Thread#run()
     */
    public abstract void run();
}

结论就是:

1、效果上没区别,写法上的区别而已。
2、没有可比性,Thread实现了Runnable接口并进行了扩展,我们通常拿来进行比较只是写法上的比较,而Thread和Runnable的实质是实现的关系,不是同类东西。

无论你使用Runnable还是Thread,都有一个new Thread的过程,效果上最后都是new Thread,然后执行run方法。写法上的区别无非就是你是new Thead还是new你自定义的thread,如果你有复杂的线程操作需求,那就自定义Thread,如果只是简单的在子线程run一下任务,那就自己实现runnable,当然如果自己实现runnable的话可以多一个继承(自定义Thread必须继承Thread类,java单继承规定导致不能在继承别的了)。

 

posted @ 2019-11-08 10:03  yaphetsfang  阅读(302)  评论(0编辑  收藏  举报