Compare Runnable and Thread

Look at the code

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; } } } } 

Output:

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  

So much sold half of the votes is obviously unreasonable, with runnable try:

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; } } } } 

Output:

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

This result was reasonable.
Obviously this case is completely wrong, mostly because selling tickets is not because of the difference Runnable and Thread, call to see to know.
When using Thread is called:

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

Runnable is used when this call:

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

New MyThread the two objects to sell tickets do not sell tickets twice when pigs fly, when selling tickets and Runnable Runnable object is the same, certainly sell tickets doubled, so this example did not reflect the difference between the Runnable and Thread, Let's look at an example:

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(); } } 

Output:

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

(Here it must use synchronized, there would be a case of repeated selling tickets, of course, has nothing to do this and this article, not detailed here.)
This achieves double the ticket sales results, no problems. This looks, Thread and Runnable would not be no distinction between?
To find the answer is simple, the point went to see the source code to know Thread

public class Thread implements Runnable {} 

As can be seen, Thread achieve Runnable interface, and which in the example above MyThread2 the same, when we use

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

When such an approach would be considered mentally retarded. . . . Yes, that is mentally retarded, take your pants fart this is written, the standard wording should be:

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

Look at the source:

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

. new Thread (new TicketThread () , " Thread 1") start (); such an approach which, TicketThread will be treated as a Runnable, then I have to write a class TicketThread doing? So that's your pants off fart.
Having said that, it is what in the end is the difference between the two?
Take a look at Runnable code:

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(); } 

The conclusion is:

1, no difference in effect, the difference between the wording of it.
2, are not comparable, and Thread achieve Runnable interface is extended, we usually used to compare just more on the wording, but the essence is the relationship Thread and Runnable implementation, not the same thing.

Whether you use Runnable or Thread, there is a new Thread process, the final results are new Thread, and then execute the run method. The difference is that you are nothing more than writing new Thead or new thread your custom, if you have a complicated threading operation needs, then customize Thread, if only a simple task in the child thread run it, it would realize his runnable, of course if they achieve a multi-runnable, then you can inherit (custom Thread Thread class must inherit, java lead single inheritance provisions can not inherit anything else).

 

Guess you like

Origin www.cnblogs.com/yaphetsfang/p/11818385.html