Java学习day097 并发(七)Callable 与 Future

使用的教材是java核心技术卷1,我将跟着这本书的章节同时配合视频资源来进行学习基础java知识。

day097   并发(七)Callable 与 Future


Runnable封装一个异步运行的任务,可以把它想象成为一个没有参数和返回值的异步方法。Callable与Runnable类似,但是有返回值。Callable接口是一个参数化的类型,只有一个方法call。

public interface Ca11able<V>
{
    V call() throws Exception;
}

类型参数是返回值的类型。例如,Callable表示一个最终返回Integer对象的异步计算。Future保存异步计算的结果。可以启动一个计算,

将Future对象交给某个线程,然后忘掉它。Future对象的所有者在结果计算好之后就可以获得它。

Future接口具有下面的方法:

public interface Future<V>
{
    V get() throws . .
    V get(long timeout, TimeUnit unit) throws ..
    void cancel(boolean maylnterrupt);
    boolean isCancelled();
    boolean isDone();
}

第一个get方法的调用被阻塞,直到计算完成。如果在计算完成之前,第二个方法的调用超时,拋出一个TimeoutException异常。如果运行该计算的线程被中断,两个方法都将拋出IntermptedException。如果计算已经完成,那么get方法立即返回。

如果计算还在进行,isDone方法返回false;如果完成了,则返回true。

可以用cancel方法取消该计算。如果计算还没有开始,它被取消且不再开始。如果计算处于运行之中,那么如果maylnterrupt参数为true,它就被中断。

FutureTask包装器是一种非常便利的机制,可将Callable转换成Future和Runnable,它同时实现二者的接口。例如:

Callable<Integer>myComputation = . . .;
FutureTask<Integer>task = new FutureTask<Integer>(myConiputation);
Thread t = new Thread(task); //it's a Runnable
t.start();
...
Integer result = task.get();// it's a Future

下面的程序使用了这些概念。这个程序与前面那个寻找包含指定关键字的文件的例子相似。然而,现在我们仅仅计算匹配的文件数目。因此,我们有了一个需要长时间运行的任务,它产生一个整数值,一个Callable<Integer>的例子。

class MatchCounter implements Callable<Integer 〉
{
    public MatchCounter(File directory, String keyword) { ...}
    public Integer call() { . . . }//returns the number of matching files
}

然后我们利用 MatchCounter 创建一个 FutureTask 对象,并用来启动一个线程。

FutureTask<Integer>task = new FutureTask<Integer>(counter);
Thread t = new Thread(task);
t.start();

最后,我们打印结果。

System.out.println(task.get()+"matchingfiles.");

当然,对get的调用会发生阻塞,直到有可获得的结果为止。在call方法内部,使用相同的递归机制。对于每一个子目录,我们产生一个新的MatchCounter并为它启动一个线程。此外,把FutureTask对象隐藏在ArrayList<Future<Integer>>中。最后,把所有结果加起来:

for(Future<Integer> result:results)

count +=result.get();

每一次对get的调用都会发生阻塞直到结果可获得为止。当然,线程是并行运行的,因此,很可能在大致相同的时刻所有的结果都可获得。

/**
 *@author  zzehao
 */
import java.io.*; 
import java.util.*; 
import java.util.concurrent.*;

public class  FutureTest
{
	public static void main(String[] args)
	{
		try(Scanner in = new Scanner(System.in))
		{
			System.out.print("Enter base directory (e.g. /opt/jdkl.8.0/src): ");
			String directory = in.nextLine();
			System.out.print("Enter keyword (e.g. volatile): ");
			String keyword= in.nextLine();

			MatchCounter counter = new MatchCounter(new File(directory), keyword);
			FutureTask<Integer> task = new FutureTask<>(counter);
			Thread t = new Thread(task);
			t.start();
			try
			{
				System.out.println(task.get()+ " matching files.");
			}
			catch (ExecutionException e)
			{
				e.printStackTrace();
			}
			catch (InterruptedException e)
			{
			}
		}
	}
}

//This task counts the files in a directory and its subdirectories that contain a given keyword.
class MatchCounter implements Callable<Integer>
{
	private File directory;
	private String keyword;

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

	public Integer call()
	{
		int 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> result : results)
			{
				try
				{
					count += result.get();
				}
				catch (ExecutionException e)
				{
					e.printStackTrace();
				}
			}
		}
		catch (InterruptedException e)
		{
		}
		return count;
	}

	//Searches a file for a given keyword.
	public boolean search(File file)
	{
		try
		{
			try (Scanner in = new Scanner(file, "UTF-8"))
			{
				boolean found = false;
				while (!found && in.hasNextLine())
				{
					String line = in.nextLine();
					if (line.contains(keyword)) 
						found = true;
				}
				return found;
			}
		}
		catch (IOException e)
		{
			return false;
		}
	}
}

运行的结果:


 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值