在线程异步的场合下,如何将线程信息传递到调用处(1)

简介: 在线程异步的场合下,如何将线程信息传递到调用处

本篇我们来学习一下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());

}

相关文章
|
3月前
|
Python
【Python30天速成计划】10.异步以及多进程和多线程
【Python30天速成计划】10.异步以及多进程和多线程
|
7月前
|
数据采集 Java Python
多线程与多任务异步协程高效爬虫
多线程与多任务异步协程高效爬虫
|
7月前
|
数据采集 Python
使用多线程或异步技术提高图片抓取效率
图片抓取是爬虫技术中常见的需求,但是图片抓取的效率受到很多因素的影响,比如网速、网站反爬机制、图片数量和大小等。本文将介绍如何使用多线程或异步技术来提高图片抓取的效率,以及如何使用爬虫代理IP来避免被网站拒绝服务
使用多线程或异步技术提高图片抓取效率
|
30天前
|
Python
Python学习之路 02 之分支结构
Python学习之路 02 之分支结构
47 0
Python学习之路 02 之分支结构
|
30天前
|
Java Python 开发者
Python 学习之路 01基础入门---【Python安装,Python程序基本组成】
线程池详解与异步任务编排使用案例-xian-cheng-chi-xiang-jie-yu-yi-bu-ren-wu-bian-pai-shi-yong-an-li
78 2
Python 学习之路 01基础入门---【Python安装,Python程序基本组成】
|
25天前
|
JavaScript 前端开发
JS 单线程还是多线程,如何显示异步操作
JS 单线程还是多线程,如何显示异步操作
22 2
|
1月前
|
JavaScript Java API
spring boot使用异步多线程
一文讲清楚spring boot如何结合异步多线程实现文件的导出这类耗时间的操作优化以及常用的场景,了解异步思想
31 0
spring boot使用异步多线程
|
1月前
|
Java
多线程------Future异步任务
多线程------Future异步任务
|
6月前
|
并行计算 安全 Java
【JavaSE专栏80】多线程通信,多个线程之间如何实现信息传递和同步?
【JavaSE专栏80】多线程通信,多个线程之间如何实现信息传递和同步?
|
3月前
|
存储 JSON 运维
【运维】Powershell 服务器系统管理信息总结(进程、线程、磁盘、内存、网络、CPU、持续运行时间、系统账户、日志事件)
【运维】Powershell 服务器系统管理信息总结(进程、线程、磁盘、内存、网络、CPU、持续运行时间、系统账户、日志事件)
49 0