JavaのCallableとFuture実装スレッド待ち


1、Callable:
public interface Callable<V>
 
  

返回结果并且可能抛出异常的任务。实现者定义了一个不带任何参数的叫做 call 的方法。

Callable 接口类似于 Runnable,两者都是为那些其实例可能被另一个线程执行的类设计的。但是 Runnable 不会返回结果,并且无法抛出经过检查的异常。

Executors 类包含一些从其他普通形式转换成 Callable 类的实用方法。

call

V call()
       throws Exception

結果を計算し、結果を計算できない場合は例外を放出します. 
戻り値:
計算結果
放出:Exception  - 結果を計算できない場合
2、Future:
public interface Future<V>
 
  

Future 表示异步计算的结果。它提供了检查计算是否完成的方法,以等待计算的完成,并获取计算的结果。计算完成后只能使用 get 方法来获取结果,如有必要,计算完成前可以阻塞此方法。取消则由 cancel 方法来执行。还提供了其他方法,以确定任务是正常完成还是被取消了。一旦计算完成,就不能再取消计算。如果为了可取消性而使用 Future 但又不提供可用的结果,则可以声明 Future<?> 形式类型、并返回 null 作为底层任务的结果。

get

V get()
      throws InterruptedException,
             ExecutionException

必要に応じて、計算が完了するまで待機し、結果を取得します. 
戻り値:
計算結果
放出:CancellationException  - 計算がキャンセルされた場合ExecutionException  - 計算が例外を放出した場合InterruptedException  - 現在のスレッドが待機中に中断された場合
3、関連例:
<pre name="code" class="java">public class TestFutureTask {
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		final ExecutorService exec = Executors.newFixedThreadPool(5);
		Callable<String> call = new Callable<String>() {
			public String call() throws Exception {
				Thread.sleep(1000 * 3);//        ,           
				return "Other less important but longtime things.";
			}
		};
		Future<String> task = exec.submit(call);
		//Thread.sleep(1000 * 3);
		System.out.println("Let's do important things.");
		//              ,        ,    
		boolean isDone = task.isDone();
		while(!isDone){
			isDone = task.isDone();
		}
		String obj = task.get();
		System.out.println(obj);
		//      
		exec.shutdown();
	}
}
:
 
  
Let's do important things.
Other less important but longtime things.
  :http://www.itzhai.com/callable-and-future-realization-of-the-thread-waiting-for.html
 
  
  
public class TestFutureTask2 {
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		final ExecutorService exec = Executors.newFixedThreadPool(5);
		Runnable runnable = new Runnable() {
			
			@Override
			public void run() {
				try {
					Thread.sleep(1000 * 2);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}//        ,           
			}
		};
		Future<String> task = exec.submit(runnable, "aa");


		System.out.println("Let's do important things.");
		//              ,        ,    
		boolean isDone = task.isDone();
		while(!isDone){
			//Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds,
 Thread.sleep(500);
			isDone = task.isDone();
		}
		String obj = task.get();
		System.out.println(obj);//  aa,   submit       
		//      
		exec.shutdown();
	}
}
 
  
 
  
public class TestFutureTask3 {
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		final ExecutorService exec = Executors.newFixedThreadPool(5);
		Runnable runnable = new Runnable() {
			
			@Override
			public void run() {
				try {
					Thread.sleep(1000 * 2);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}//        ,           
			}
		};
		Future<?> task = exec.submit(runnable);


		System.out.println("Let's do important things.");
		//       
		//              ,        ,    
		boolean isDone = task.isDone();
		while(!isDone){
			//Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds,
			Thread.sleep(500);
			isDone = task.isDone();
		}
		String obj = (String) task.get();
		System.out.println(obj);//  null
		//      
		exec.shutdown();
	}
}