本篇我们来学习一下Java是如何获取线程的信息然后返回到调用线程处(学习书籍(Java网络编程)):
1.首先,我们来学习一个简单的线程,继承Thread类,然后输出文件的摘要信息
public class DigestThread extends Thread { private File input; // 通过构造方法,我们将file对象传递到run方法 public DigestThread(File input) { this.input = input; } public void run() { try { FileInputStream in = new FileInputStream(this.input); // 消息摘要 MessageDigest sha = MessageDigest.getInstance("SHA"); DigestInputStream din = new DigestInputStream(in, sha); // 要完成消息摘要计算,先要调用此摘要输入流的一个 read 方法,之后在关联的消息摘要上调用一个 digest 方法。 while ((din.read()) != -1); din.close(); byte [] digest = sha.digest(); StringBuffer result = new StringBuffer(input.toString()); result.append(": "); for (int i = 0; i < digest.length; i++) { result.append(digest[i] + " "); } // 输出 System.out.println(result.toString()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { Thread thread = new DigestThread(new File("D:\\test\\demo01.html")); thread.start(); } }
3.单线程和多线程让程序员棘手的问题就是如何将线程的信息传递回线程的调用处,在以上实例中,我们只是把线程中获取的消息摘要输出,假如说我们要把摘要返回到主线程调用处,我们该怎么做呢。多数情况下,我们可以进行了一下操作,使用变量存储
3.1.线程类
private File input; public ReturnDigestThread(File input) { this.input = input; } // 将摘要存储到该变量中,使用get set方法进行获取 private byte [] digest; public void run() { try { FileInputStream in = new FileInputStream(this.input); // 消息摘要 MessageDigest sha = MessageDigest.getInstance("SHA"); DigestInputStream din = new DigestInputStream(in, sha); // 要完成消息摘要计算,先要调用此摘要输入流的一个 read 方法,之后在关联的消息摘要上调用一个 digest 方法。 while ((din.read()) != -1); din.close(); // 设置到results变量中 setDigest(sha.digest()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public byte[] getDigest() { return digest; } public void setDigest(byte[] digest) { this.digest = digest; }
3.2.主线程类
public static void main(String[] args) { File input = new File("D:\\test\\demo01.html"); ReturnDigestThread thread = new ReturnDigestThread(input); thread.start(); // 输出 StringBuffer result = new StringBuffer(input.toString()); result.append(": "); // 通过对象的变量获取线程的执行结果 for (int i = 0; i < thread.getDigest().length; i++) { result.append(thread.getDigest()[i] + " "); } System.out.println(result.toString()); }
错误 写道
Exception in thread "main" java.lang.NullPointerException
at network.ReturnDigestUserInterface.main(ReturnDigestUserInterface.java:18)
修改主线程 写道
public static void main(String[] args) throws InterruptedException {
File input = new File("D:\\test\\demo01.html");
ReturnDigestThread thread = new ReturnDigestThread(input);
thread.start();
// 这样做只是可能会得到了我们想要的结果,但是,依然是错误的做法
Thread.sleep(100);
// 输出
StringBuffer result = new StringBuffer(input.toString());
result.append(": ");
// 通过对象的变量获取线程的执行结果
for (int i = 0; i < thread.getDigest().length; i++) {
result.append(thread.getDigest()[i] + " ");
}
System.out.println(result.toString());
}