Java线程间通信-回调的实现方式
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 CallbackDigest implements Runnable {
private File inputFile; //目标文件
public CallbackDigest(File input) {
this.inputFile = input;
}
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(); //摘要码
//完成后,回调主线程静态方法,将文件名-摘要码结果传递给住线程
CallbackDigestUserInterface.receiveDigest(digest, inputFile.getName());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 静态非同步回调
*
* @author leizhimin 2008-9-11 23:00:12
*/
public class CallbackDigestUserInterface {
/**
* 接收摘要码,输出到控制台
*
* @param digest 摘要码
* @param inputFileName 输入的文件名
*/
public static void receiveDigest(byte[] digest, String inputFileName) {
StringBuffer result = new StringBuffer(inputFileName);
result.append(": ");
for (int j = 0; j < digest.length; j++) {
result.append(digest[j] + " ");
}
System.out.println(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]);
CallbackDigest cb = new CallbackDigest(f);
Thread t = new Thread(cb);
t.start();
}
}
}
xb.txt: 112 -81 113 94 -65 -101 46 -24 -83 -55 -115 18 -1 91 -97 98
x.txt: 123 -91 90 -16 -116 -94 -29 -5 -73 25 -57 12 71 23 -8 -47
xcopy.txt: 123 -91 90 -16 -116 -94 -29 -5 -73 25 -57 12 71 23 -8 -47
Process finished with exit code 0
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();
}
}
}
/**
* 静态非同步回调
*
* @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();
}
}
}
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
calculateDigest()方法,这个方法可能在逻辑上认为它属于一个构造器。然而,在构造器中启动线程是相当危险的,特别是对开始对象回调的线
程。这里存在一个竞争条件:构造器中假如有很多的事情要做,而启动新的线程先做了,计算完成了后要回调,可是这个时候这个对象还没有初始化完成,这样就产
生了错误。当然,实际中我还没有发现这样的错误,但是理论上是可能的。 因此,避免从构造器中启动线程是一个明智的选择。
* 回调接口
*
* @author leizhimin 2008-9-13 17:20:11
*/
public interface DigestListener {
public void digestCalculated(byte digest[]);
}
* Created by IntelliJ IDEA.
*
* @author leizhimin 2008-9-13 17:22:00
*/
public class ListCallbackDigest implements Runnable {
private File inputFile;
private List<DigestListener> listenerList = Collections.synchronizedList(new ArrayList<DigestListener>());
public ListCallbackDigest(File inputFile) {
this.inputFile = inputFile;
}
public synchronized void addDigestListener(DigestListener ler) {
listenerList.add(ler);
}
public synchronized void removeDigestListener(DigestListener ler) {
listenerList.remove(ler);
}
private synchronized void sendDigest(byte digest[]) {
for (DigestListener ler : listenerList) {
ler.digestCalculated(digest);
}
}
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(); //摘要码
//完成后,回调主线程静态方法,将文件名-摘要码结果传递给住线程
System.out.println(digest);
this.sendDigest(digest);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
* Created by IntelliJ IDEA.
*
* @author leizhimin 2008-9-13 17:35:20
*/
public class ListCallbackDigestUser implements DigestListener{
private File inputFile; //回调与每个文件绑定
private byte digest[]; //文件的消息摘要码
public ListCallbackDigestUser(File inputFile) {
this.inputFile = inputFile;
}
/**
* 计算某个文件的消息摘要码
*/
public void calculateDigest() {
ListCallbackDigest callback = new ListCallbackDigest(inputFile);
Thread t = new Thread(callback);
t.start();
}
public void digestCalculated(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]);
ListCallbackDigestUser cb = new ListCallbackDigestUser(f);
cb.calculateDigest();
}
}
}
Java线程间通信-回调的实现方式的更多相关文章
- Java线程间通信之wait/notify
Java中的wait/notify/notifyAll可用来实现线程间通信,是Object类的方法,这三个方法都是native方法,是平台相关的,常用来实现生产者/消费者模式.我们来看下相关定义: w ...
- java线程间通信:一个小Demo完全搞懂
版权声明:本文出自汪磊的博客,转载请务必注明出处. Java线程系列文章只是自己知识的总结梳理,都是最基础的玩意,已经掌握熟练的可以绕过. 一.从一个小Demo说起 上篇我们聊到了Java多线程的同步 ...
- 说说Java线程间通信
序言 正文 [一] Java线程间如何通信? 线程间通信的目标是使线程间能够互相发送信号,包括如下几种方式: 1.通过共享对象通信 线程间发送信号的一个简单方式是在共享对象的变量里设置信号值:线程A在 ...
- 说说 Java 线程间通信
序言 正文 一.Java线程间如何通信? 线程间通信的目标是使线程间能够互相发送信号,包括如下几种方式: 1.通过共享对象通信 线程间发送信号的一个简单方式是在共享对象的变量里设置信号值:线程A在一个 ...
- Java——线程间通信
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- VC 线程间通信的三种方式
1.使用全局变量(窗体不适用) 实现线程间通信的方法有很多,常用的主要是通过全局变量.自定义消息和事件对象等来实现的.其中又以对全局变量的使用最为简洁.该方法将全局变量作为线程监视的对象,并通 ...
- 【转】VC 线程间通信的三种方式
原文网址:http://my.oschina.net/laopiao/blog/94728 1.使用全局变量(窗体不适用) 实现线程间通信的方法有很多,常用的主要是通过全局变量.自定义消息和 ...
- 线程间通信的三种方式(NSThread,GCD,NSOperation)
一.NSThread线程间通信 #import "ViewController.h" @interface ViewController ()<UIScrollViewDel ...
- java线程间通信1--简单实例
线程通信 一.线程间通信的条件 1.两个以上的线程访问同一块内存 2.线程同步,关键字 synchronized 二.线程间通信主要涉及的方法 wait(); ----> 用于阻塞进程 noti ...
随机推荐
- 安装protobuf及相关的lua生成器
1.google code 需要用到的水星:http://mercurial.selenic.com/ 2.protobuf地址 https://code.google.com/p/protobuf/ ...
- javascript设计模式2
接口:利 固化一部分代码 弊 丧失js的灵活性 在JavaScript中模仿接口 /* interface Composite{ function add(child); function remov ...
- Weka 入门2
现在我们介绍使用Weka来对数据进行分类.对数据进行分类,我们必须先指定那一列作为预测类别.因为数据文件格式的问题,类别一般都是最后一列属性.我们可以使用setClassIndex来设置类别.然后我们 ...
- HW1.5
public class Solution { public static void main(String[] args) { System.out.println("(9.5 * 4.5 ...
- 恢复oracle中误删除drop掉的表
查看回收站中表 select object_name,original_name,partition_name,type,ts_name,createtime,droptime from recycl ...
- mac使用初级
imac使用的是login shell,所有开启一个terminal的时候,不会运行.bashrc文件,而是运行.bash_profile文件,因此只需要中home目录新建一个.bash_profil ...
- HDU5627--Clarke and MST (bfs+位运算)
http://www.cnblogs.com/wenruo/p/5188495.html Clarke and MST Time Limit: 2000/1000 MS (Java/Others) M ...
- Java 线程池详细介绍
根据摩尔定律(Moore’s law),集成电路晶体管的数量差不多每两年就会翻一倍.但是晶体管数量指数级的增长不一定会导致 CPU 性能的指数级增长.处理器制造商花了很多年来提高时钟频率和指令并行.在 ...
- 《机器学习实战》——K近邻算法
三要素:距离度量.k值选择.分类决策 原理: (1) 输入点A,输入已知分类的数据集data (2) 求A与数据集中每个点的距离,归一化,并排序,选择距离最近的前K个点 (3) K个点进行投票,票数最 ...
- 24C02操作--松瀚汇编源码
; ; P_CLKIIC EQU P1.2 ; P_DATIIC EQU P1.3 ; PM_DATIIC EQU P1M.3 ; EE_ADDR DS 1 ;地址寄存器 ; TMP3_IIC DS ...