個人賬戶類:
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
葉澍成 閱讀(235)
評論(0) 編輯 收藏 所屬分類:
NIO學習 、
多線程