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

          主函數測試:

          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 葉澍成 閱讀(242) 評論(0)  編輯  收藏 所屬分類: NIO學習多線程
          主站蜘蛛池模板: 江川县| 丹棱县| 永济市| 故城县| 老河口市| 洛川县| 波密县| 西峡县| 宁安市| 彝良县| 双鸭山市| 加查县| 柘荣县| 旬邑县| 东丰县| 青川县| 康马县| 麟游县| 大同市| 育儿| 晋中市| 唐海县| 精河县| 永年县| 西藏| 石河子市| 新宾| 云阳县| 武宣县| 淅川县| 宁国市| 临漳县| 龙里县| 安仁县| 廊坊市| 安远县| 镇雄县| 丰宁| 永安市| 收藏| 鞍山市|