java to create thread in several ways

1. Create a thread by implementing Runnable interface

Package com.javaBase.LineDistance; 

/ ** 
 * <Function Description word>; 
 * <detailed description Function> 
 * 
 * @author JXX 
 * @see [Class / Method] (optional) 
 * @Since [Product / module version] (optional)
  * / 
public  class testRunn the implements the Runnable { 


    @Override 
    public  void RUN () { 
        System.out.println ( "123" ); 
    } 
}
Package com.javaBase.LineDistance; 

/ ** 
 * <Function Description word>; 
 * <multithreaded test code> 
 * 
 * @author JXX 
 * @see [Class / Method] (optional) 
 * @Since [Product / module version] (optional)
  * / 
public  class TestLine { 

    public  static  void main (String [] args) {   
        the Thread T = new new the Thread ( new new testRunn ()); 
        t.start (); 
    } 
}

2. By implementing callable interface to create threads

Package com.javaBase.LineDistance; 

Import java.util.concurrent.Callable; 

/ ** 
 * <Function Description word>; 
 * <detailed description Function> 
 * 
 * @author JXX 
 * @see [Class / Method] (available option) 
 * @Since [product / module versions] (optional)
  * / 
public  class TestCallable the implements a Callable { 

    @Override 
    public Object Call () throws Exception { 
        System.out.println ( "123" );
         return ". 1" ; 
    } 
}
Package com.javaBase.LineDistance; 

Import java.util.concurrent.Callable;
 Import java.util.concurrent.FutureTask; 

/ ** 
 * <Function Description word>; 
 * <multithreaded test code> 
 * 
 * @author JXX 
 * @see [Related class / method] (optional) 
 * @Since [product / module versions] (optional)
  * / 
public  class TestLine { 

    public  static  void main (String [] args) { 
        a Callable Call = new new TestCallable (); 
        Future a FutureTask, = new new a FutureTask, (Call); 
        the Thread T2= new Thread(new testRunn());
        t2.start();
    }
}

3. By integrating the Thread class to create a thread

Package com.javaBase.LineDistance; 

/ ** 
 * <Function Description word>; 
 * <detailed description Function> 
 * 
 * @author JXX 
 * @see [Class / Method] (optional) 
 * @Since [Product / module version] (optional)
  * / 
public  class TestThread the extends the Thread { 

    @Override 
    public  void RUN () { 
        System.out.println ( "123" ); 
    } 

    public  static  void main (String [] args) { 
        the Thread T = new new TestThread (); 
        t.start (); 
    } 
}

4. Use the Executor framework to create a thread pool

  After Java 5, concurrent programming introduces a bunch of new startup, scheduling and thread management API. Executor frame is introduced in Java 5, the internal mechanism using a thread pool, which at java.util.cocurrent packet start threads controlled by the frame, and the closed execution, concurrent programming operation can be simplified. Therefore, after Java 5, to start the thread Thread the start better than using methods Executor, except easier to manage, better efficiency (with the thread pool implementation, cost savings), there is the key point: this helps to avoid escape question - if we start a thread in the constructor, because another task may begin before the end of the constructor, this time may have access to half of the objects initialized with the Executor in the constructor.

Package com.javaBase.LineDistance; 

Import java.util.concurrent.ExecutorService;
 Import java.util.concurrent.Executors; 

/ ** 
 * <Function Description word>; 
 * <detailed description Function> 
 * 
 * @author JXX 
 * @ See [class / method] (optional) 
 * @Since [product / module versions] (optional)
  * / 
public  class TestExcetor { 

    public  static  void main (String [] args) { 
        ExecutorService ExecutorService = Executors.newCachedThreadPool (); 
        executorService.execute ( new new testRunn ());
        executorService.shutdown();
    }
}

Detailed Executor frame back again.

Callable and the difference 5.Runnable

  • Callable predetermined method is call (), the predetermined method is Runnable run ()
  • Callable after the execution of the task can return a value, but can not return Runnable task is worth
  • call method can throw an exception, run method can not
  • Run Callable task can get a Future object that represents the result of an asynchronous computation. It provides a method of checking computation is complete, to wait for the completion of calculation, and calculation of the search result. By Future object can understand the implementation of the mandate, to cancel the execution of the task, but also get the results.

 

Guess you like

Origin www.cnblogs.com/jxxblogs/p/11646830.html