The difference between creating a thread of three ways _Callable and Runnable

Java provides three ways to create a thread

  1. By implementing Runnable interface
  2. Through inheritance Thread Interface
  3. Create a thread through Callable and Future

To create a thread by implementing Runnable interface

public  class RunnableDemo {
     public  static  void main (String [] args) {
         new new the Thread ( new new the MyThread (), " thread. 1 " ) .start ();
         new new the Thread ( new new the Runnable () { 
            @Override 
            public  void RUN () {
                 / / the TODO Auto-Generated Method Stub 
                
            } 
        }, " thread 2 " ) .start (); 
    } 
} 

/ * * 
 * implement Runnable thread classes 
 * / 
class MyThread implements Runnable{

    /** 
     * 重写run方法
     */
    @Override
    public void run() {
        // TODO Auto-generated method stub
        
    }
}

 

Through inheritance Thread class

public class ThreadDemo {
    public static void main(String[] args) {
        new MyThread().start();
        new Thread(new MyThread(), "线程2").start();
    }
}
class MyThread extends Thread{

    /** 
     * 重写run方法
     */
     @Override
     public void run() {
         // TODO Auto-generated method stub
         super.run();
     }
}

 

Create a thread through Callable and Future

public  class FutureDemo {
     public  static  void main (String [] args) {
         // create an instance of the implementation class Callable 
        MyCallable myCallable = new new MyCallable ();
         // use FutureTask packaging Callable object class to which the object encapsulates FutureTask object Callable call () method returns the value 
        FutureTask <String> = FutureTask new new FutureTask <String> (myCallable); 
        String RES = null ;
         the try {
             // use FutureTask thread object as target object and starts to create a new thread
             // no sentence, The following sentence code gets less than the result would have been waiting for the results of 
            newThread (futureTask, "thread. 1" ) .start ();
             // call FutureTask object get () method returns the value obtained after completion of the sub-thread execution 
            RES = futureTask.get (); 
        } the catch (Exception E) { 
            E .printStackTrace (); 
        } 
        System.out.println (RES); 
    } 
} 
/ ** 
 * Create Callable interface implementation class, and implements call () method 
 * / 
class MyCallable the implements Callable <String> { 

    / **  
     * the call () method as a thread of execution, and a return value 
     * / 
    @Override 
    public String Call () throws Exception {
         return "success";
    }
}

 Runnable and Callable the similarities and differences:

Same point:

  1. Both interfaces; (nonsense)
  2. Both can be used to write multi-threaded programs;
  3. Both need to call Thread.start () start the thread;

difference:

  1. Both the biggest difference is: implement Callable interface task execution thread can return result; and implement Runnable task thread can not return result;
  2. Callable call interface () method allows an exception is thrown; and run Runnable interface () method is an exception only in the internal digestion, can not continue on the throw;

important point:

  • Callable interface supports the implementation of the results returned, this time need to call FutureTask.get () method implementation, this method blocks until the main thread to obtain 'the future' results; when not call this method, the main thread does not block!

Callable advantage

  Multi-threaded execution result returned is a useful feature, because the more difficult compared to single-threaded multi-threaded, a more important reason is because the complex multi-thread full of unknown sex, whether a certain threads? Some threads execute how long? Whether certain threads when the execution of an assignment we expect the data has been completed? Not know, we can do is wait this multi-threaded task is finished it. Under the circumstances Callable + Future / FutureTask but you can get the results of running multiple threads, you can not wait too long to get the data needed to cancel the mandate of the thread, is really very useful.

 

Guess you like

Origin www.cnblogs.com/sunweiye/p/11023571.html