2010年11月2日

               摘要: 用java實現(xiàn)的日期,精確到分秒  閱讀全文
          posted @ 2010-11-02 14:59 buptduming 閱讀(2434) | 評論 (0)編輯 收藏

          2010年9月7日

               摘要: java中的指針思想  閱讀全文
          posted @ 2010-09-07 21:21 buptduming 閱讀(327) | 評論 (0)編輯 收藏

          2010年8月1日

               摘要: java 異步socket  閱讀全文
          posted @ 2010-08-01 21:19 buptduming 閱讀(4502) | 評論 (0)編輯 收藏

          2010年7月20日

               摘要: 介紹了java 中UDP 傳遞消息  閱讀全文
          posted @ 2010-07-20 21:19 buptduming 閱讀(2382) | 評論 (1)編輯 收藏

          2010年7月16日

               摘要: java實現(xiàn)了socket連接池  閱讀全文
          posted @ 2010-07-16 22:37 buptduming 閱讀(5318) | 評論 (9)編輯 收藏

          2010年7月14日

               摘要: 使用java創(chuàng)建線程池  閱讀全文
          posted @ 2010-07-14 21:08 buptduming 閱讀(517) | 評論 (0)編輯 收藏
           
               摘要: 接口列表實現(xiàn) 線程與主類的交互。  閱讀全文
          posted @ 2010-07-14 15:06 buptduming 閱讀(265) | 評論 (0)編輯 收藏
           
           一 概述 
                 在上一篇隨筆中使用的是靜態(tài)回調函數(shù),故Thread類中沒有主類的實例對象。
                但是如果調用主類的實例方法,那么在線程類中就需要有主類的實例對象, 這個實例對象一般是通過線程類的構造函數(shù)中的參數(shù)傳遞的。
             
           二 代碼 
                 和上一篇隨筆代碼相似,只是在線程類中增加了一個主類 的一個實例變量,通過該實例來調用主類的實例函數(shù)。

             1   線程類
                          
                  成員變量中增加一個主類的成員變量callBack。 

                 
           1package cn.bupt.thread;
           2
           3import java.io.*;
           4import java.security.DigestInputStream;
           5import java.security.MessageDigest;
           6import java.security.NoSuchAlgorithmException;
           7
           8import cn.bupt.test.CallbackDigestUserInterface;
           9
          10public class ThreadDemo implements Runnable{
          11
          12    private File input ;
          13    private byte [] digest ;
          14    private CallbackDigestUserInterface callBack ;
          15    public ThreadDemo(File input , CallbackDigestUserInterface callBack)
          16    {
          17        this.input = input ;
          18        this.callBack = callBack ;
          19    }

          20    
          21    public byte [] getDigest()
          22    {
          23        return digest ;
          24    }

          25    
          26    
          27    @Override
          28    public void run() {
          29    
          30        try {
          31            FileInputStream in = new FileInputStream(input) ;
          32            MessageDigest sha = MessageDigest.getInstance("SHA") ;
          33            DigestInputStream din = new DigestInputStream(in , sha) ;
          34            
          35            int b ;
          36            
          37            while((b =  din.read()) != -1) ;
          38            din.close() ;
          39            
          40            digest = sha.digest() ;
          41            
          42            callBack.receiveDigest(digest, input.getName()) ;
          43            
          44        }
           catch (FileNotFoundException e) {
          45            // TODO Auto-generated catch block
          46            e.printStackTrace();
          47        }
           catch (NoSuchAlgorithmException e) {
          48            // TODO Auto-generated catch block
          49            e.printStackTrace();
          50        }
           catch (IOException e) {
          51            // TODO Auto-generated catch block
          52            e.printStackTrace();
          53        }

          54        
          55        
          56        
          57        
          58    }

          59
          60     
          61    
          62}

          63
           
             2  主類 
              
                 主類通過線程類的構造函數(shù)將 自身對象傳遞進去 。

               
          package cn.bupt.test;

          import java.io.File;

          import cn.bupt.thread.ThreadDemo;

          public class CallbackDigestUserInterface {

              
          public  void receiveDigest(byte [] digest , String name){
                  
                  StringBuffer result 
          = new StringBuffer(name) ;
                  result.append(
          "") ;
                  
          for(int j = 0 ; j < digest.length ; j++){
                      result.append(digest[j] 
          + " ") ;        
                  }

                  System.out.println(
          "result :" + result) ;
              }

              
          /**
               * 
          @param args
               
          */

              
          public static void main(String[] args) {
                  
          // TODO Auto-generated method stub        
                  for(int i = 0 ; i < args.length ; i++)
                  
          {
                      File f 
          = new File(args[i]) ;
                      CallbackDigestUserInterface u 
          = new CallbackDigestUserInterface() ;
                      ThreadDemo td 
          = new ThreadDemo(f , u) ;
                      Thread t 
          = new Thread(td) ;
                      t.start() ;
                  }
                  
              }

          }


            

           3 優(yōu)點 
              回調機制可以處理涉及更多的線程,對象和類的更復雜的情況,如果有多個對象關心線程的執(zhí)行結果
              那么線程可以保存一個回調函數(shù)列表,某個對象可以通過調用thread類 或者 Runnable 的方法,把自己添加
              到列表中,。
              
              如果有多個類的實例關心結果,那么就可以定義一個新的interface ,所有這些類都應該實現(xiàn)這個新的接口,
             











                    
           
          posted @ 2010-07-14 14:14 buptduming 閱讀(265) | 評論 (0)編輯 收藏
           
               摘要: 使用回調實例方法進行線程與主類的交互  閱讀全文
          posted @ 2010-07-14 14:14 buptduming 閱讀(275) | 評論 (0)編輯 收藏
           
               摘要: 回調函數(shù)實現(xiàn)線程類和主類之間的數(shù)據(jù)交互  閱讀全文
          posted @ 2010-07-14 11:28 buptduming 閱讀(795) | 評論 (0)編輯 收藏
          僅列出標題  下一頁
           
          主站蜘蛛池模板: 桂东县| 达尔| 南开区| 如东县| 榆林市| 资溪县| 金阳县| 清河县| 高阳县| 舒城县| 安图县| 阳谷县| 涟水县| 乐安县| 连云港市| 萝北县| 大城县| 阜新| 筠连县| 兴宁市| 徐州市| 达拉特旗| 东光县| 璧山县| 晋中市| 万州区| 沁源县| 阳东县| 中超| 商丘市| 天峻县| 甘南县| 邵武市| 汉源县| 拜城县| 泽州县| 卢龙县| 武川县| 凉城县| 乌兰浩特市| 仁怀市|