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 ...
随机推荐
- [King.yue]Ext.JS 弹出窗体取值赋值
//从Grid取值 var name = Ext.getCmp(gridGridID).getView().getSelectionModel().getSelection()[0].data.Nam ...
- 输入一个正数 n,输出所有和为 n 连续正数序列。 java实现
用了半小时才写出来啊, 其实这种思路应用范围很广,利用有序性这个特点,比如本体只要是有序就行,例如两个集合是有序的,求交集,有序数组的查找,有序数组的归并,有序有了优化的可能. 输入一个正数 n,输出 ...
- linux内核申请内存函数
kmap函数: 把某块高端内存映射到页表,然后返回给用户一个填好vitual字段的page结构 建立永久地址映射,不是简单的返回virtual字段的pageioremap: 驱动程序 ...
- NOIP2001 统计单词个数
题三 统计单词个数(30分) 问题描述 给出一个长度不超过200的由小写英文字母组成的字母串(约定;该字串以每行20个字母的方式输入,且保证每行一定为20个).要求将此字母串分成k份(1<k&l ...
- Log Parser 微软强大的日志分析工具
Log Parser(微软网站下载)是微软公司出品的日志分析工具,它功能强大,使用简单,可以分析基于文本的日志文件.XML 文件.CSV(逗号分隔符)文件,以及操作系统的事件日志.注册表.文件系统.A ...
- devexpress中gridview控件编辑时改变输入法状态
在win7环境下使用Devexpress中的SpinEdit控件,切换成中文[简/繁]输入法输入数字键时有不少输入法会重复产生数字如输入1会变成11,输入123会变成112233.使用SpinEdit ...
- [置顶] 自己写sqlhelper类
自己写sqlhelper类 using System; using System.Collections.Generic; using System.Configuration; using Syst ...
- golang中赋值string到array
要把一个string赋值给一个array,哥哥遇到一个纠结的困难,研究一番,发现主要原因是array和slice在golang里不是一个东西,本文提供两种解决方案. 在网络编程中network pac ...
- AS3 IOC框架Spring Actionscript 的使用总结
Spring Actionscript 是众多围绕依赖注入提供解决方案的Flex控制反转框架之一 AS3 下经典的IOC框架有Spring ActionScript.Parsley.Flicc和Swi ...
- OC中 self.view.frame.size.height = 100; 不能通过编译的原因
在OC中,当需要修改一个view的尺寸时,通常是通过先将 self.view.fram赋值给一个临时变量,然后修改临时变量,最后将临时变量赋值给 self.view.frame.代码如下: // 1. ...