Thread ways:
1. Thread class inherits
2. implement Runnable
3. Thread pool
4.Callable
Regardless of inheritance Thread class or implement Runnable interface, or use the thread pool is no way to solve two problems
1. thread of execution does not return a result value
2. Thread execution is no way to throw an exception, can only be resolved through their own try-catch
Callable and Runnable Similarly, at JUC package, the main difference lies in the method call Callable may return value and can throw exceptions
if desired Callable performed, need to support Future implementation class, capable of receiving the return value result, Future implementation class is a FutureTask,
Callable first call of implementation:
public class MyCallable the implements Callable <String> { @Override public String Call () throws Exception { System.out.println ( "Call Callable interface rewriting method, there may be a return value and throws an exception" ); return "Callable" ; } public static void main (String [] args) throws ExecutionException, InterruptedException { MyCallable myCallable = new new MyCallable (); //Callable use FutureTask execution and accepts the results FutureTask <String> = stringFutureTask new new FutureTask <> (myCallable); // use the thread to perform tasks Task new new the Thread (stringFutureTask) .start (); // accept the results FutureTask.get clog happens System .out.println (stringFutureTask.get ()); System.out.println ( "MyCallable finished, return the result value received correctly ~" ); } }
A second call Callable implementation:
public static void main (String [] args) throws ExecutionException, InterruptedException { MyCallable myCallable = new new MyCallable (); // create a thread pool ExecutorService ExecutorService = Executors.newFixedThreadPool (3 ); // create a thread to perform the task, given the task results Future <String> Future = ExecutorService.submit (myCallable); // accepts return value System.out.println (Future.get (2000 , TimeUnit.MILLISECONDS)); System.out.println ("Mode 2 thread pool: MyCallable finished, return the result value received correctly ~" ); // stop thread pool executorService.shutdown (); }
Future.get () method to get the results of task execution, when the method if no return, temporarily blocked state
Future.get (Long timeOut, TimeUnit timeUnit) can set the timeout
Future.boolean isDone () if the thread end, whether it is the end of normal or termination of the task will return true
Future.boolean isCanceller () is canceled before the task is completed if it returns true
Future.boolean the cancel (boolean Flag) method if the incoming parameter is true representatives of interrupt task, if the task is interrupted succeeds, the return value is true , and false if it fails
Future provides three functions: 1 interrupt task cancel (true) 2. determine whether the task is completed isDone () 3. Get the results get the task execution ()
// interrupt task Boolean Cancel = Future.cancel ( to true ); IF (Cancel) { System.out.println ( "interrupt task successfully ~" ); } the else { // accept the return value System.out.println (future.get (2000 , TimeUnit.MILLISECONDS)); }
If the handwriting Future models should be how to define?
wait in charge of obstruction and notify responsible evoke blocked thread
public class MyFuture { // the FLAG corresponds to the identification data, if the data into successful, returns to true, otherwise returns to false Private static Boolean the FLAG = to false ; Private String Data; public the synchronized void the setData (String Data) throws InterruptedException { the Thread .sleep ( 2000 ); // assignment the this .data = Data; the FLAG = to true ; // arouse notify(); } public synchronized String getData() { //如果获取数据失败 if(!FLAG){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } return data; } public static void main(String[] args) { MyFuture future=new MyFuture(); new Thread(()->{ try { future.setData("张三"); System.out.println(future.getData()); } catch (InterruptedException e) { e.printStackTrace(); } }).start(); } }