java concurrent之Runnable,Callable,Executor

Runnable作为线程的接口已经广为大家所知。

但是在执行任务的时候如果想要返回结果怎么办?如果想抛出异常怎么办?

这些都是Runnable无法做到的。这时候另一个线程接口就出现了: Callable:

 V call() throws Exception;

 Oh,perfect! 可以返回值,可以抛异常!

 

我们知道Runnable是通过Thread 类来执行的:new Thread(new Runnable(){..}).start();

那么Callable呢?

这个是通过RunnableFuture来执行的(当然RunnableFuture也可以用来执行Runnable)。

RunnableFuture的一个常用子类是:FutureTask.

下面的代码演示了这几个接口的用法:

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;


@SuppressWarnings("unchecked")
public class TestTask {
private FutureTask<String> task=new FutureTask(new Callable(){
	public String call() throws Exception {
		return "hello";
	}
}
);
private ExecutorService exeService=Executors.newFixedThreadPool(4);
public static void main(String args[]){
	TestTask test=new TestTask();
	test.testFuture();
	test.testExecutor();
}
public void testFuture(){
	task.run();
	String result="";
	try {
		result = task.get();
	} catch (InterruptedException e) {
		e.printStackTrace();
	} catch (ExecutionException e) {
		e.printStackTrace();
	}
	System.out.println("task returns :"+result);
}
public void testExecutor(){
	exeService.execute(task);
	String result="";
	try {
		result = task.get();
	} catch (InterruptedException e) {
		e.printStackTrace();
	} catch (ExecutionException e) {
		e.printStackTrace();
	}
	System.out.println("task submmited and run:"+result);
}
}

 细心的看官可能已经发现了,这里面用到了另一个类:ExecutorService.

 这是java concurrent执行框架的一部分。

 这个框架包含了Executor(能执行Runnable),ExecutorService(Executor的子类,加强了对Runnable生命周期的管理),Executors(Executor和ExecutorService的工厂方法).在上面是通过Executors的静态方法产生一个

ThreadPoolExecutor来执行Runnable.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值