您好,欢迎访问一九零五行业门户网

java-并发-Callable、Future和FutureTask

创建线程的2种方式,一种是直接继承thread,另外一种就是实现runnable接口
区别:接口可以实现多继承
缺陷就是:在执行完任务之后无法获取执行结果
callable与runnable
java.lang.runnable
[code]public interface runnable { public abstract void run(); }
由于run()方法返回值为void类型,所以在执行完任务之后无法返回任何结果。
java.util.concurren
[code]public interface callable<v> { /** * computes a result, or throws an exception if unable to do so. * * @return computed result * @throws exception if unable to compute a result */ v call() throws exception; }
泛型接口,call()函数返回的类型就是传递进来的v类型
一般情况下是配合executorservice来使用的,在executorservice接口中声明了若干个submit方法的重载版本
[code]<t> future<t> submit(callable<t> task); <t> future<t> submit(runnable task, t result); future<?> submit(runnable task);
future
future就是对于具体的runnable或者callable任务的执行结果进行取消、查询是否完成、获取结果。必要时可以通过get方法获取执行结果,该方法会阻塞直到任务返回结果
[code]public interface future<v> { boolean cancel(boolean mayinterruptifrunning); boolean iscancelled(); boolean isdone(); v get() throws interruptedexception, executionexception; v get(long timeout, timeunit unit) throws interruptedexception, executionexception, timeoutexception; }
cancel方法用来取消任务,如果取消任务成功则返回true,如果取消任务失败则返回false。参数mayinterruptifrunning表示是否允许取消正在执行却没有执行完毕的任务,如果设置true,则表示可以取消正在执行过程中的任务。如果任务已经完成,则无论mayinterruptifrunning为true还是false,此方法肯定返回false,即如果取消已经完成的任务会返回false;如果任务正在执行,若mayinterruptifrunning设置为true,则返回true,若mayinterruptifrunning设置为false,则返回false;如果任务还没有执行,则无论mayinterruptifrunning为true还是false,肯定返回true。
iscancelled方法表示任务是否被取消成功,如果在任务正常完成前被取消成功,则返回 true。
isdone方法表示任务是否已经完成,若任务完成,则返回true;
get()方法用来获取执行结果,这个方法会产生阻塞,会一直等到任务执行完毕才返回;
get(long timeout, timeunit unit)用来获取执行结果,如果在指定时间内,还没获取到结果,就直接返回null。
也就是说future提供了三种功能:
1)判断任务是否完成;
2)能够中断任务;
3)能够获取任务执行结果。
因为future只是一个接口,所以是无法直接用来创建对象使用的,因此就有了下面的futuretask。
futuretask
[code]public class futuretask<v> implements runnablefuture<v>
[code]public interface runnablefuture<v> extends runnable, future<v> { void run(); }
[code]public futuretask(callable<v> callable) { } public futuretask(runnable runnable, v result) { }
事实上,futuretask是future接口的一个唯一实现类。
例子
[code]public class test { public static void main(string[] args) { executorservice executor = executors.newcachedthreadpool(); task task = new task(); future<integer> result = executor.submit(task); executor.shutdown(); try { thread.sleep(1000); } catch (interruptedexception e1) { e1.printstacktrace(); } system.out.println("主线程在执行任务"); try { system.out.println("task运行结果"+result.get()); } catch (interruptedexception e) { e.printstacktrace(); } catch (executionexception e) { e.printstacktrace(); } system.out.println("所有任务执行完毕"); } } class task implements callable<integer>{ @override public integer call() throws exception { system.out.println("子线程在进行计算"); thread.sleep(3000); int sum = 0; for(int i=0;i<100;i++) sum += i; return sum; } }
[code]public class test { public static void main(string[] args) { //第一种方式 executorservice executor = executors.newcachedthreadpool(); task task = new task(); futuretask<integer> futuretask = new futuretask<integer>(task); executor.submit(futuretask); executor.shutdown(); //第二种方式,注意这种方式和第一种方式效果是类似的,只不过一个使用的是executorservice,一个使用的是thread /*task task = new task(); futuretask<integer> futuretask = new futuretask<integer>(task); thread thread = new thread(futuretask); thread.start();*/ try { thread.sleep(1000); } catch (interruptedexception e1) { e1.printstacktrace(); } system.out.println("主线程在执行任务"); try { system.out.println("task运行结果"+futuretask.get()); } catch (interruptedexception e) { e.printstacktrace(); } catch (executionexception e) { e.printstacktrace(); } system.out.println("所有任务执行完毕"); } } class task implements callable<integer>{ @override public integer call() throws exception { system.out.println("子线程在进行计算"); thread.sleep(3000); int sum = 0; for(int i=0;i<100;i++) sum += i; return sum; } }
如果为了可取消性而使用 future 但又不提供可用的结果,则可以声明 future
以上就是java-并发-callable、future和futuretask的内容。
其它类似信息

推荐信息