Callable和Future

Callable

相對於Runnable,Callable並非很讓人熟知,其實Callable和Runnable很相似,只不過它有返回值,而且也沒有run()方法,而是有call()方法。java

public interface Callable<V>{
    V call() throw Exception;
}

Callable 接口相似於 Runnable,二者都是爲那些其實例可能被另外一個線程執行的類設計的。可是 Runnable 不會返回結果,而且沒法拋出通過檢查的異常。異步

能夠看到,返回的結果是以V泛型表示的,好比Callable<Integer>表示一個最終返回Integer的異步計算ide

Future

Future用來保存異步計算的結果,就是說以前用Callable標誌的任務能夠用Future來進行包裝,那爲何非要用Future呢,Callable本身運行而後用相應的類型來接收結果不就好了嗎?之因此要用到Future,有一下兩個緣由:測試

  1. Thread t = new Thread(..)用這個方法建立一個線程,必需要傳給一個Runnable的參數,而不能傳給它Callable
  2. Future對象具備一系列的方法,好比它的get方法可以被阻塞,直到計算完成;也能夠在任務進行的過程當中取消它。這些方法使得它更便利

FutureTask

可是Future終究只是一個接口,而FutureTask包裝器不只實現了Future接口,還實現了Runnable接口,這彌補上面的遺憾,使得它不只能被Thread運行,還具備取消運行的特性,一個典型的使用FutureTask的例子就是:this

Callable<Integer> calc = ...;
FutureTask<Integer> task = new FutureTask<Integer>(calc);
Thread t = new Thread(task);   //這裏是Runnable
t.start;
...
Integer result = task.get();   //這裏是Future

住:FutureTask的兩個構造方法spa

FutureTask(Callable<V> task)線程

FutureTask(Runnable task,V result)設計

Demo

一個計算指定目錄下具備指定關鍵字的文件數目的例子:code

package future;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

public class MatchCounter implements Callable<Integer> {

	private File directory;
	private String keyword;
	private int count;

	public MatchCounter(File directory, String keyword) {
		this.directory = directory;
		this.keyword = keyword;
	}

	@Override
	public Integer call() throws Exception {
		count = 0;
		try {
			File[] files = directory.listFiles();
			List<Future<Integer>> results = new ArrayList<>(); // 用來保存全部的異步計算結果

			for (File file : files) {
				if (file.isDirectory()) {
					MatchCounter counter = new MatchCounter(file, keyword);
					FutureTask<Integer> task = new FutureTask<>(counter);
					results.add(task);
					Thread t = new Thread(task);
					t.start();
				} else {
					if (search(file)) {
						count++;
					}
				}
			}

			for (Future<Integer> r : results) {
				count += r.get();
			}
		} catch (InterruptedException e) {

		}
		return count;
	}

	public boolean search(File file) {
		try {
			Scanner in = new Scanner(file);
			boolean found = false;
			while (in.hasNextLine()) {
				String line = in.nextLine();
				if (line.contains(keyword)) {
					found = true;
				}
			}
			return found;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return false;

	}
}

測試類:對象

package future;

import java.io.File;
import java.util.Scanner;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class FutureTest {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter starting directory:");
		String d = in.nextLine();
		System.out.println("Enter keyword:");
		String keyword = in.nextLine();

		MatchCounter counter = new MatchCounter(new File(d), keyword);
		FutureTask<Integer> task = new FutureTask<>(counter);

		new Thread(task).start();

		try {
			System.out.println(task.get() + " matching files!");
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
	}
}

運行:

Enter starting directory:
D:\workspace\concurrent\src
Enter keyword:
future
2 matching files!
相關文章
相關標籤/搜索