62,621
社区成员
发帖
与我相关
我的任务
分享
public abstract class FutureProxy{
private final class CallableImpl implements Callable{
public Object call() throws Exception {
return FutureProxy.this.createInstance();
}
}
private static class InvocationHandlerImpl implements InvocationHandler {
private Future future;
private volatile Object instance;
InvocationHandlerImpl(Future future){
this.future = future;
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
synchronized(this){
if(this.future.isDone()){
this.instance = this.future.get();
}else{
while(!this.future.isDone()){
try{
this.instance = this.future.get();
}catch(InterruptedException e){
Thread.currentThread().interrupt();
}
}
}
return method.invoke(this.instance, args);
}
}
}
private static final class ThreadFactoryImpl implements ThreadFactory {
public Thread newThread(Runnable r) {
Thread thread = new Thread(r);
thread.setDaemon(true);
return thread;
}
}
private static ExecutorService service = Executors.newCachedThreadPool(new ThreadFactoryImpl());
protected abstract Object createInstance();
protected abstract Class getInterface();
public final Object getProxyInstance() {
Class interfaceClass = this.getInterface();
if (interfaceClass == null || !interfaceClass.isInterface()) {
throw new IllegalStateException();
}
Callable task = new CallableImpl();
Future future = FutureProxy.service.submit(task);
return (Object) Proxy.newProxyInstance(interfaceClass.getClassLoader(),
new Class[] { interfaceClass }, new InvocationHandlerImpl(future));
}
}
interface DateTest{
String getDate();
}
class DateTestImpl implements DateTest{
private String _date=null;
public DateTestImpl(){
try{
_date+=Calendar.getInstance().getTime();
}catch(Exception e){
}
}
public String getDate() {
//A.method是一个费时操作,比如读取数据库之类的……
A a=new A();
String s="";
if(a.method()){
s="a:"+Calendar.getInstance().getTime();
}else{
s="b:"+Calendar.getInstance().getTime();
}
return "date "+_date+":"+s;
}
}
class DateTestFactory extends FutureProxy{
protected Object createInstance() {
return new DateTestImpl();
}
protected Class getInterface() {
return DateTest.class;
}
}
public class Test{
public static void main(String[] args) {
DateTestFactory factory = new DateTestFactory();
DateTest[] dts = new DateTest[10];
for(int i=0;i<dts.length;i++){
dts[i]=(DateTest)factory.getProxyInstance();
}
for(int i=0;i<dts.length;i++){
System.out.println(dts[i].getDate());
}
}
}
Tue Jul 07 10:35:20 CST 2009
Tue Jul 07 10:35:25 CST 2009
Tue Jul 07 10:35:30 CST 2009
import java.util.*;
import java.util.concurrent.*;
public class CallableExample {
public static class WordLengthCallable
implements Callable {
private String word;
public WordLengthCallable(String word) {
this.word = word;
}
public Integer call() {
return Integer.valueOf(word.length());
}
}
public static void main(String args[]) throws Exception {
ExecutorService pool = Executors.newFixedThreadPool(3);
Set<Future<Integer>> set = new HashSet<Future≶Integer>>();
for (String word: args) {
Callable<Integer> callable = new WordLengthCallable(word);
Future<Integer> future = pool.submit(callable);
set.add(future);
}
int sum = 0;
for (Future<Integer> future : set) {
sum += future.get();
}
System.out.printf("The sum of lengths is %s%n", sum);
System.exit(sum);
}
}