转自:http://programmingexamples.wikidot.com/futuretask
A cancellable asynchronous computation. This class provides a base implementation of Future, with methods to start and cancel a computation, query to see if the computation is complete, and retrieve the result of the computation. The result can only be retrieved when the computation has completed; the get method will block if the computation has not yet completed. Once the computation has completed, the computation cannot be restarted or cancelled.[1]
The following example illustrates the use of the Futuretask together with the Callable.
It reverses the submitted string slowly at a rate of one char per second. In the mean time, the main thread keeps polling to see if the task is completed.
import java.util.concurrent.*;
import java.util.concurrent.ExecutorService;
public class MyStringReverser
{
ExecutorService executor = Executors.newFixedThreadPool(1);
SlowStringReverser reverser = new SlowStringReverser();
void doReverse(final String target) throws InterruptedException
{
FutureTask<String> future = new FutureTask<String>(
new Callable<String>()
{
public String call()
{
return reverser.reverseString(target);
}
});
executor.execute(future);
// try every 10 seconds
while (!future.isDone())
{
System.out.println("Task not yet completed.");
try
{
Thread.sleep(500);
} catch (InterruptedException ie)
{
System.out.println("Will check after 1/2 sec.");
}
}
try
{
System.out.println("Here is the result..." + future.get());
} catch (ExecutionException ex)
{
}
executor.shutdown();
return;
}
public static void main(String args[])
{
MyStringReverser msr = new MyStringReverser();
try
{
msr.doReverse("foobar");
} catch (Exception e)
{
e.printStackTrace();
}
}
}
class SlowStringReverser
{
StringBuffer orgString;
StringBuffer reversedString;
SlowStringReverser(String orgStr)
{
orgString = new StringBuffer(orgStr);
}
SlowStringReverser()
{
}
public String reverseString(String str)
{
orgString = new StringBuffer(str);
reversedString = new StringBuffer();
for (int i = (orgString.length() - 1); i >= 0; i--)
{
reversedString.append(orgString.charAt(i));
System.out.println("Reversing one character per second."
+ reversedString);
try
{
Thread.sleep(1000);
} catch (InterruptedException ie)
{
}
}
return reversedString.toString();
}
}
分享到:
相关推荐
主要介绍了futuretask用法及使用场景介绍,小编觉得挺不错的,这里分享给大家,供大家参考。
主要介绍了简谈java并发FutureTask的实现,FutureTask都是用于获取线程执行的返回结果。文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,,需要的朋友可以参考下
最代码,http://www.zuidaima.com/share/1724478138158080.htm 的代码及例子
NULL 博文链接:https://dingran.iteye.com/blog/1864962
主要介绍了Java中的Runnable,Callable,Future,FutureTask的比较的相关资料,需要的朋友可以参考下
FutureTask原始码解析 一,FutureTask是什么? FutureTask是可取消的异步的计算任务,它可以通过线程池和线程对象执行,一般来说是FutureTask用于耗时的计算。 二,FutureTask继承图 三,未来任务源码 FutureTask的...
主要介绍了Java线程池FutureTask实现原理详解,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
在本篇文章里我们给大家分享了java中Future与FutureTask之间的关系的内容,有需要的朋友们可以跟着学习下。
FutureTask底层实现分析,有了FutureTask主线程要想获得工作线程(异步计算线程)的结果变得比较简单
主要介绍了futuretask源码分析(推荐),小编觉得还是挺不错的,这里给大家分享下,供各位参考。
主要介绍了Java FutureTask类使用案例解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值析,需要的朋友可以参考下
主要为大家详细介绍了Java中Future、FutureTask原理以及与线程池的搭配使用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Future Future是一个接口,它定义了5个方法: boolean cancel(boolean mayInterruptIfRunning); boolean isCancelled(); boolean isDone(); V get() throws InterruptedException, ExecutionException;...
Java 多线程与并发(17_26)-JUC线程池_ FutureTask详解
通过合规策略对服务器进行监控,确保服务器的运行、帐号在服务器上的操作符合预设的规则。日志:收集、整理服务器的日志信息,提供给管理员查看,并作为异常判断、故障排查的依据。进程:监控服务器上的进程,并对...
tiny-asyncload 看源码时,根据其原理模仿实现的一套future task 优雅调用的框架,主要用于学习futureTask,以及加深对动态代理的理解.
这是一个集齐了runnable与callnable的线程处理包,自动集齐全部功能,只需引用即可
JAVA线程总结,包含线程池,显示使用线程实现异步编程,基于JDK中的Future实现异步编程,JDK中的FutureTask等
http://blog.csdn.net/yangzhaomuma/article/details/51722779
计算机技术、IT咨询、人工智能AI理论介绍,学习参考资料 计算机技术、IT咨询、人工智能AI理论介绍,学习参考资料 计算机技术、IT咨询、人工智能AI理论介绍,学习参考资料 计算机技术、IT咨询、人工智能AI理论介绍,...