利用回调函数在线程和主类中传递消息

一  回调函数

      采用回调函数,让线程通知主程序何时结束,它通过调用主程序中的一个方法来实现。
     (线程就是在该主类中启动的) ,这就称为回调,因为线程在结束的时候,调用其创建者。这样
     主程序就可以在线程运行的时候休息,而不会占用线程的时间。

  二 线程类实现 

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

18    
19    public byte [] getDigest()
20    {
21        return digest ;
22    }

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

52        
53        
54        
55        
56    }

57
58     
59    
60}

61



     在上面代码中的第40行,调用了主类中的一个静态函数 ,而实质上这一静态函数是在线程中运行的。
         
    三 主类中的代码 
         
          主类中的main函数实质上只是启动每一个线程,在线程中的静态函数实质上是在thread类中调用

          
 1 package  cn.bupt.test;
 2
 3 import  java.io.File;
 4
 5 import  cn.bupt.thread.ThreadDemo;
 6
 7 public   class  CallbackDigestUserInterface  {
 8
 9    public static void receiveDigest(byte [] digest , String name){
10        
11        StringBuffer result = new StringBuffer(name) ;
12        result.append("") ;
13        for(int j = 0 ; j < digest.length ; j++){
14            result.append(digest[j] + " ") ;        
15        }

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

18    /** *//**
19     * @param args
20     */

21    public static void main(String[] args) {
22        // TODO Auto-generated method stub        
23        for(int i = 0 ; i < args.length ; i++)
24        {
25            File f = new File(args[i]) ;
26            ThreadDemo td = new ThreadDemo(f) ;
27            Thread t = new Thread(td) ;
28            t.start() ;
29        }
        
30    }

31}

32



   四 总结
         回调函数并没有想象中的那么复杂,只是在thread的run方法的结束前,调用主类中定义的方法。
         方法需要synchronized 的限制。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值