如何异步调用一个java方法,

定义一个类,实现 Runnable 接口,将任务实现代码放在 run 方法中,然后启动该线程。

如果该任务完成后要通知主线程,使用回调函数。





====

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.DigestInputStream;

/**
* 求文件的信息摘要码(MD5)
*
* @author leizhimin 2008-9-11 22:53:39
*/

public class InstanceCallbackDigest implements Runnable {
     private File inputFile;         //目标文件
     //每个线程绑定一个回调对象
     private InstanceCallbackDigestUserInterface instanceCallback;

     /**
     * 构件时一次注入回调对象
     *
     * @param instanceCallback
     * @param inputFile
     */

     public InstanceCallbackDigest(InstanceCallbackDigestUserInterface instanceCallback, File inputFile) {
         this.instanceCallback = instanceCallback;
         this.inputFile = inputFile;
    }

     public void run() {
         try {
            FileInputStream in = new FileInputStream(inputFile);
            MessageDigest sha = MessageDigest.getInstance( "MD5");
            DigestInputStream din = new DigestInputStream(in, sha);
             int b;
             while ((b = din.read()) != -1) ;
            din.close();
             byte[] digest = sha.digest();   //摘要码
             //完成后,回调主线程静态方法,将文件名-摘要码结果传递给住线程  
            instanceCallback.receiveDigest(digest);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}


import java.io.File;

/**
* 静态非同步回调
*
* @author leizhimin 2008-9-11 23:00:12
*/

public class InstanceCallbackDigestUserInterface {
    private File inputFile;     //回调与每个文件绑定
    private byte digest[];      //文件的消息摘要码

    public InstanceCallbackDigestUserInterface(File inputFile) {
        this.inputFile = inputFile;
    }

    /**
     * 计算某个文件的消息摘要码
     */

    public void calculateDigest() {
        InstanceCallbackDigest callback = new InstanceCallbackDigest(this, inputFile);
        Thread t = new Thread(callback);
        t.start();
    }

    /**
     * 接收消息摘要码
     *
     * @param digest
     */

    public void receiveDigest(byte[] digest) {
        this.digest = digest;
        //将消息摘要码输出到控制台实际上执行的是this.toString()方法
        System.out.println(this);
    }

    /**
     * 显示结果
     *
     * @return 结果
     */

    public String toString() {
        String result = inputFile.getName() + ": ";
        if (digest != null) {
            for (byte b : digest) {
                result += b + " ";
            }
        } else {
            result += "digest 不可用!";
        }
        return result;
    }

    public static void main(String[] args) {
        String arr[] = {"C:\\xcopy.txt", "C:\\x.txt", "C:\\xb.txt", "C:\\bf2.txt"};
        args = arr;
        for (int i = 0; i < args.length; i++) {
            File f = new File(args[i]);
            InstanceCallbackDigestUserInterface cb = new InstanceCallbackDigestUserInterface(f);
            cb.calculateDigest();
        }
    }
}



结果:

xcopy.txt: 123 -91 90 -16 -116 -94 -29 -5 -73 25 -57 12 71 23 -8 -47  
x.txt: 123 -91 90 -16 -116 -94 -29 -5 -73 25 -57 12 71 23 -8 -47  
xb.txt: 112 -81 113 94 -65 -101 46 -24 -83 -55 -115 18 -1 91 -97 98  
bf2.txt: 31 -37 46 -53 -26 -45 36 -105 -89 124 119 111 28 72 74 112  

Process finished with exit code 0

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值