正确停止在InputStream的read()中被阻塞的线程

时间:2014-06-11 11:20:01

标签: java multithreading stream

我的线程从InputStream读取,构建对象并将它们放入队列中。

如果在read()中阻止该线程,我应该如何阻止它?

public class InputEventReader extends Thread {

  private final BlockingQueue<ButtonEvent> eventQueue;
  private File name = new File("/dev/input/event0");
  private DataInputStream in;

  private volatile boolean run = true;

  public InputEventReader(BlockingQueue<ButtonEvent> mainButtonQueue) {
    this.eventQueue = mainButtonQueue;
  }

  public void run() {
    in = new DataInputStream(
           new FileInputStream(name));
    byte[] buffer = new byte[16];

    while (run) {
      in.readFully(buffer); // blocks here
      ButtonEvent event = new ButtonEvent(buffer);
      eventQueue.offer(event);
    }
    in.close();
  }

  public void shutdown(){
    run = false; 
    try {
      this.join(500); // Thread is blocked in read() while no data arrives, so "run" is not checked  
      in.close(); // has no effect on blocked read()
      this.join(500); 
      this.interrupt(); // has no effect on blocked read()
      this.join(500);
      if(this.isAlive()){
        // Yes, the thread is still alive here...
        // How to shut it down?
      }
    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}

该平台是ARM v7 hardfloat上的linux 3.12。

JVM是

java version "1.7.0_51" 
Java(TM) SE Embedded Runtime Environment (build 1.7.0_51-b13, headless) 
Java HotSpot(TM) Embedded Client VM (build 24.51-b03, mixed mode)

2 个答案:

答案 0 :(得分:1)

考虑使用FileChannel来阅读您的数据,因为它是Interruptible

答案 1 :(得分:0)

使用DIS的readFully()方法时请务必小心,因为它等待读取指定数量的数据。相反,read()方法将尝试读取指定数量的数据,但如果读取的字节数更少,则会返回。如果可能,请尝试使用read()而不是readFully()。 如果您必须使用阻止读取,请查看以下内容:http://hypirion.com/musings/how-to-cancel-a-blocking-read