如何异步调用一个java方法,
2014-10-29 11:09
543 查看
定义一个类,实现 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
如果该任务完成后要通知主线程,使用回调函数。
====
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
相关文章推荐
- Java中如何通过一个类名来调用另一个类的静态方法?
- JAVA如何调用另外一个类的方法
- 如何在一个java类main方法中调用另一个类中的main方法
- java中如何让一个子类不能调用从父类继承的方法
- java一个方法形参有两个,如何在调用的时候只传入一个参数
- 如何在Java中实现远程方法调用
- 如何应用Java的链式方法调用 推荐
- java创建一个子类对象是会调用父类的构造方法会不会创建父类
- 如何在Java中实现远程方法调用
- 如何在一个窗体中调用另一个窗体的控件或方法
- AS、Flex如何循环调用一个对象的属性、方法
- 在EXE文件中如何实现调用另外一个EXE文件中的方法?
- .NET调用Java方法时出现的一个错误及其原因
- 如何异步调用 Visual C# 方法
- java 如何调用com组件接口方法
- 如何在java中调用js方法
- 如何用C#创建一个类并在其他Web页中调用该类的方法(2)
- 如何用C#创建一个类并在其他Web页中调用该类的方法(1)
- Java调用内部类成员函数的一个方法
- flex 中通过 RO 调用一个java写的Bean的时候获得IP地址的方法 .也就是获得session 和 request的方法