yeshucheng
          追逐自己,追逐方向,心隨悟所動
          posts - 24,comments - 24,trackbacks - 0

          個人賬戶類:

          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;
              }
          }

          主函數(shù)測試:

          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);
                          
          //創(chuàng)建新線程獲取個人賬戶信息
                  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()){//判斷是否已經(jīng)獲取返回值
                      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(
          "您當(dāng)前賬戶共有金額為:"+(total+post)+" ¥");
              }

          }


           

           

          posted on 2010-12-10 20:53 葉澍成 閱讀(237) 評論(0)  編輯  收藏 所屬分類: NIO學(xué)習(xí)多線程
          主站蜘蛛池模板: 鹤壁市| 济南市| 蓝山县| 达拉特旗| 高淳县| 荥阳市| 永丰县| 民丰县| 南澳县| 井陉县| 玉林市| 仲巴县| 通江县| 黄陵县| 额敏县| 乳源| 耿马| 宜川县| 安宁市| 德钦县| 合川市| 永康市| 东山县| 临安市| 五常市| 红桥区| 交口县| 西乌| 怀集县| 新绛县| 自治县| 太康县| 西贡区| 临海市| 天等县| 高平市| 嘉善县| 遂平县| 南川市| 府谷县| 塔城市|