package com.zhoubo.concurrent.future;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/**
* Preloader creates a FutureTask that describes the task of loading product information from a database
* and a thread in which the computation will be performed.
* When the program later needs the ProductInfo, it can call get, which returns the loaded data if it is ready,
* or waits for the load to complete if not.
* @author Administrator
*
*/
public class Preloader {
//FutureTask implements the Runnable and Future Task
private final FutureTask<ProductInfo> future = new FutureTask<ProductInfo>(
new Callable<ProductInfo>() {
public ProductInfo call() throws DataLoadException {
return loadProductInfo();
}
});
private final Thread thread = new Thread(future);
/**
* It provides a start method to start the thread,
* since it is inadvisable to start a thread from a constructor or static initializer
*/
public void start() {
thread.start();
}
public ProductInfo loadProductInfo(){
try{
//使得加载数据延迟,让等待get的现象变得明显
Thread.currentThread().sleep(2000);
}catch (Exception e) {
e.printStackTrace();
}
//int k=3/0;//make an exception
return new ProductInfo("happy every day");
}
public ProductInfo get() throws DataLoadException, InterruptedException {
try {
// future.cancel(true);
return future.get();
} catch (ExecutionException e) {
//异常处理
Throwable cause = e.getCause();
if (cause instanceof DataLoadException)
throw (DataLoadException) cause;
else
throw launderThrowable(cause);
}
}
public static RuntimeException launderThrowable(Throwable t) {
if (t instanceof RuntimeException)
return (RuntimeException) t;
else if (t instanceof Error)
throw (Error) t;
else
throw new IllegalStateException("Not unchecked", t);
}
public static void main(String ...args){
Preloader loader = new Preloader();
loader.start();
try{
System.out.println(loader.get());
}catch (Exception e) {
e.printStackTrace();
}
}
}
class ProductInfo {
private String productName;
public ProductInfo() {
// TODO Auto-generated constructor stub
}
public ProductInfo(String productName) {
this.productName = productName;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return productName;
}
}
class DataLoadException extends Exception {
}
分享到:
相关推荐
java8 源码 Java 并发多线程从简到全 ...实例,这个实例 outcome 成员变量用于存储线程的执行结果, state 成员变量用于记录线程的执行状态。 由于不知道什么时候线程执行完毕并返回结果,主线程只能主动轮询查看线程
有以下类的实例: ThreadPool ScheduledThread CyclicBarrier BlockingQueue CountDownLatch FutureTask CompletionService Semaphore
主要介绍了java多线程编程同步器Future和FutureTask解析及代码示例,对二者进行了详细介绍,分析了future的源码,最后展示了相关实例代码,具有一定参考价值 ,需要的朋友可以了解下。
task构造器,将futuretask对象传入thread构造器,用futuretask对象的get方法可以接受返回值。 使用excutors的静态方法创建线程.excute(runnable实现类)/.submit(callable实现类),.shutdown() (3)线程的生命周期...
本文将简单介绍CSP和Actor模型俩流行的并发机制,并比较他们的优缺点,并通过Golang中CSP并发机制实现FutureTask.并行机制有很多像是多线程,CSP,Actor等等.拿多线程来说,就有诸多问题,譬如:死锁,可扩展性差,共享状态....
2.实现Runnable接口,重写run方法,实现Runnable接口的实现类的实例对象作为Thread构造函数的target 3.通过Callable和FutureTask创建线程 4.通过线程池创建线程 前面两种可以归结为一类:无返回值,原因很简单,...
4.2 实例封闭 4.2.1 Java监视器模式 4.2.2 示例:车辆追踪 4.3 线程安全性的委托 4.3.1 示例:基于委托的车辆追踪器 4.3.2 独立的状态变量 4.3.3 当委托失效时 4.3.4 发布底层的状态变量 4.3.5 示例:发布...
4.2 实例封闭 4.2.1 Java监视器模式 4.2.2 示例:车辆追踪 4.3 线程安全性的委托 4.3.1 示例:基于委托的车辆追踪器 4.3.2 独立的状态变量 4.3.3 当委托失效时 4.3.4 发布底层的状态变量 4.3.5 示例:发布...
在一个App中「SmartExecutor」可以有多个实例,每个实例都有完全的「独立性」,比如独立的「核心并发」、「排队等待」指标,独立的「运行调度和满载处理」策略,但所有实例「共享一个线程池」。 这种机制既满足不同...
使用Callable对象实例化FutureTask类 信号量(Semaphore) 用来控制同时访问某个特定资源的操作数量,或者同时执行某个指定操作的数量 管理者一组虚拟的许可。acquire获得许可(相当于P操作),...