个人账户类:
public class PrivateAccount implements Callable {
Integer total;
public Object call() throws Exception {
Thread.sleep(5*1000);
total=new Integer(new Random().nextInt(10000));
System.out.println("您个人账户上还有"+total+" 存款可以支配");
return total;
}
}
主函数测试:
public class SumTest {
/**
* @param args
* @throws ExecutionException
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException, ExecutionException {
Callable privateAccount=new PrivateAccount();
FutureTask task=new FutureTask(privateAccount);
//创建新线程获取个人账户信息
Thread thread=new Thread(task);
thread.start();
int total=new Random().nextInt(1000);
System.out.println("主线程在这工作");
System.out.println("您目前操作金额为: "+total+" .");
System.out.println("请等待计算个人账户的金额");
while(!task.isDone()){//判断是否已经获取返回值
try {
Thread.sleep(3*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Integer privateSingle=(Integer)task.get();
int post=privateSingle.intValue();
System.out.println("您当前账户共有金额为:"+(total+post)+" ¥");
}
}
posted on 2010-12-10 20:53
叶澍成 阅读(220)
评论(0) 编辑 收藏 所属分类:
NIO学习 、
多线程