2010年11月2日

     摘要: 用java实现的日期,精确到分秒  阅读全文
posted @ 2010-11-02 14:59 buptduming 阅读(2413) | 评论 (0)编辑 收藏

2010年9月7日

     摘要: java中的指针思想  阅读全文
posted @ 2010-09-07 21:21 buptduming 阅读(310) | 评论 (0)编辑 收藏

2010年8月1日

     摘要: java 异步socket  阅读全文
posted @ 2010-08-01 21:19 buptduming 阅读(4475) | 评论 (0)编辑 收藏

2010年7月20日

     摘要: 介绍了java 中UDP 传递消息  阅读全文
posted @ 2010-07-20 21:19 buptduming 阅读(2363) | 评论 (1)编辑 收藏

2010年7月16日

     摘要: java实现了socket连接池  阅读全文
posted @ 2010-07-16 22:37 buptduming 阅读(5291) | 评论 (9)编辑 收藏

2010年7月14日

     摘要: 使用java创建线程池  阅读全文
posted @ 2010-07-14 21:08 buptduming 阅读(500) | 评论 (0)编辑 收藏
 
     摘要: 接口列表实现 线程与主类的交互。  阅读全文
posted @ 2010-07-14 15:06 buptduming 阅读(244) | 评论 (0)编辑 收藏
 
 一 概述 
       在上一篇随笔中使用的是静态回调函数,故Thread类中没有主类的实例对象。
      但是如果调用主类的实例方法,那么在线程类中就需要有主类的实例对象, 这个实例对象一般是通过线程类的构造函数中的参数传递的。
   
 二 代码 
       和上一篇随笔代码相似,只是在线程类中增加了一个主类 的一个实例变量,通过该实例来调用主类的实例函数。

   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  主类 
    
       主类通过线程类的构造函数将 自身对象传递进去 。

     
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 优点 
    回调机制可以处理涉及更多的线程,对象和类的更复杂的情况,如果有多个对象关心线程的执行结果
    那么线程可以保存一个回调函数列表,某个对象可以通过调用thread类 或者 Runnable 的方法,把自己添加
    到列表中,。
    
    如果有多个类的实例关心结果,那么就可以定义一个新的interface ,所有这些类都应该实现这个新的接口,
   











          
 
posted @ 2010-07-14 14:14 buptduming 阅读(247) | 评论 (0)编辑 收藏
 
     摘要: 使用回调实例方法进行线程与主类的交互  阅读全文
posted @ 2010-07-14 14:14 buptduming 阅读(257) | 评论 (0)编辑 收藏
 
     摘要: 回调函数实现线程类和主类之间的数据交互  阅读全文
posted @ 2010-07-14 11:28 buptduming 阅读(773) | 评论 (0)编辑 收藏
仅列出标题  下一页