Java 当线程被中断时,BlockingQueue方法是否总是抛出InterruptedException?

Java 当线程被中断时,BlockingQueue方法是否总是抛出InterruptedException?,java,multithreading,Java,Multithreading,在我的一个Java6应用程序中,我有一个线程向主线程提供数据,同时也从数据库中预取更多记录。它使用队列作为FIFO缓冲区,其主循环是沿着以下几行进行的: while (!Thread.interrupted()) { if (source.hasNext()) { try { queue.put(source.next()) } catch (InterruptedException e) { break;

在我的一个Java6应用程序中,我有一个线程向主线程提供数据,同时也从数据库中预取更多记录。它使用队列作为FIFO缓冲区,其主循环是沿着以下几行进行的:

while (!Thread.interrupted()) {
    if (source.hasNext()) {
        try {
            queue.put(source.next())
        } catch (InterruptedException e) {
            break;
        }
    } else {
        break;
    }
}
有一些代码在循环终止后进行了一些清理,例如对队列下毒和释放任何资源,但这几乎就是全部

目前,主线程与馈线线程之间没有直接通信:馈线线程使用适当的选项进行设置,然后独立运行,使用阻塞队列控制数据流

当队列已满时,当主线程需要关闭进料器时,就会出现此问题。由于没有直接控制通道,关机方法使用到进料器线程的接口。不幸的是,在大多数情况下,进料器线程仍然被阻塞,尽管被中断-不会引发异常

通过对文档和队列实现源代码的简要阅读,在我看来,
put()
常常在不使用JVM的任何可中断功能的情况下阻塞。更具体地说,在我当前的JVM(OpenJDK 1.6b22)上,它阻塞了
sun.misc.Unsafe.park()
native方法。也许它使用了自旋锁或其他东西,但无论如何,这似乎属于:

如果前面的条件都不成立,那么将设置该线程的中断状态

设置了一个状态标志,但线程仍然在
put()
中被阻塞,并且不会进一步迭代以检查该标志。结果如何?一条不会死的僵尸线

  • 我对这个问题的理解是正确的,还是遗漏了什么

  • 解决此问题的可能方法有哪些?现在我只能想到两种解决方案:

    a。在队列中多次调用
    poll()
    以解除对feeder线程的阻塞:从我所看到的情况来看,这很难看,也不太可靠,但它基本上是有效的

    b。使用带有超时的
    offer()
    方法,而不是
    put()
    ,以允许线程在可接受的时间范围内检查其中断状态

  • 除非我遗漏了什么,否则这是Java中BlockingQueue实现的一个有点缺乏文档的警告。当文档(例如)建议对队列下毒以关闭工作线程时,似乎有一些迹象,但我找不到任何明确的参考

    编辑:


    好的,上面溶液(a)有一个更剧烈的变化:。我认为这应该一直有效,即使这不是优雅的确切定义…

    我认为你的问题可能有两个原因

  • 如中所述,您可能没有正确处理中断。在那里你会发现:

    当调用可能导致中断异常的代码时,我们应该怎么做?不要马上拔掉电池!通常,该问题有两个答案:

    从方法中重新显示InterruptedException。这通常是最简单、最好的方法。它被新的java.util.concurrent.*包使用,这解释了为什么我们现在经常遇到这个异常。
    捕获它,设置中断状态,返回。如果在调用可能导致异常的代码的循环中运行,则应将状态设置回被中断

    例如:

    while (!Thread.currentThread().isInterrupted()) {
        // do something
        try {
            TimeUnit.SECONDS.sleep(1000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            break;
        }
    }
    
  • source.hasNext()
    source.next()
    正在使用并丢弃中断状态。有关我如何解决此问题的信息,请参见下面添加的

  • 我相信在
    ArrayBlockingqueue.put()中断线程是一个有效的解决方案

    已添加

    我使用可以从读卡器端关闭的
    CloseableBlockingQueue
    解决了问题2。这样,一旦它关闭,所有的
    put
    调用都将被删除。然后,您可以从writer检查队列的
    closed
    标志

    // A blocking queue I can close from the pull end. 
    // Please only use put because offer does not shortcut on close.
    // <editor-fold defaultstate="collapsed" desc="// Exactly what it says on the tin.">
    class CloseableBlockingQueue<E> extends ArrayBlockingQueue<E> {
      // Flag indicates closed state.
      private volatile boolean closed = false;
      // All blocked threads. Actually this is all threads that are in the process
      // of invoking a put but if put doesn't block then they disappear pretty fast.
      // NB: Container is O(1) for get and almost O(1) (depending on how busy it is) for put.
      private final Container<Thread> blocked;
    
      // Limited size.
      public CloseableBlockingQueue(int queueLength) {
        super(queueLength);
        blocked = new Container<Thread>(queueLength);
      }
    
      /**
       * *
       * Shortcut to do nothing if closed.
       *
       * Track blocked threads.
       */
      @Override
      public void put(E e) throws InterruptedException {
        if (!closed) {
          Thread t = Thread.currentThread();
          // Hold my node on the stack so removal can be trivial.
          Container.Node<Thread> n = blocked.add(t);
          try {
            super.put(e);
          } finally {
            // Not blocked anymore.
            blocked.remove(n, t);
          }
        }
      }
    
      /**
       *
       * Shortcut to do nothing if closed.
       */
      @Override
      public E poll() {
        E it = null;
        // Do nothing when closed.
        if (!closed) {
          it = super.poll();
        }
        return it;
      }
    
      /**
       *
       * Shortcut to do nothing if closed.
       */
      @Override
      public E poll(long l, TimeUnit tu) throws InterruptedException {
        E it = null;
        // Do nothing when closed.
        if (!closed) {
          it = super.poll(l, tu);
        }
        return it;
      }
    
      /**
       *
       * isClosed
       */
      boolean isClosed() {
        return closed;
      }
    
      /**
       *
       * Close down everything.
       */
      void close() {
        // Stop all new queue entries.
        closed = true;
        // Must unblock all blocked threads.
    
        // Walk all blocked threads and interrupt them.
        for (Thread t : blocked) {
          //log("! Interrupting " + t.toString());
          // Interrupt all of them.
          t.interrupt();
        }
      }
    
      @Override
      public String toString() {
        return blocked.toString();
      }
    }
    
    私有AtomicBoolean shutdown=新的AtomicBoolean(); 无效关机() { 关机。设置(真); } 而(!shutdown.get()){ if(source.hasNext()){ 对象项=source.next(); 而(!shutdown.get()&&!queue.offer(项目,100,时间单位.毫秒)){ 继续; } } 否则{ 打破 } }
    我已经轻易地放弃了这种可能性,因为进料器线程在
    put()
    中等待的时间要长得多。然而,这听起来似乎是有道理的。
    source
    对象属于第三方数据库相关库-对于所有这些网络代码,必须在某个地方抛出InterruptedException,但是顶级方法不会抛出它们。。。唉,我讨厌挖第三方代码…哦,因为我哭得太大声了。。。无论是谁编写了这个库,它都会吞下抛出的每一个中断异常!每一个人!谁编写了这样的代码?顺便说一句,将线程状态设置回interrupted(中断)不会改变与此特定线程相关的任何内容-一旦循环中断直接通向终止的道路。无论如何,我没有得到任何要处理的异常……我已经添加了我的
    CloseablBlockingQueue
    code。只要检查喂入器线程中的
    关闭
    状态,一切都应该正常。在
    close
    上,如果线程在
    put
    上被阻塞,它将得到中断。如果没有,它将默默地消耗所有的
    put
    s,直到您注意到它关闭。我将+1并接受这个答案,因为它指出了我已经放弃的可能性,并以代码的形式提供了一个全面的解决方案:-)我不完全相信第三方库对我看到的内容负有全部责任,但我真的没有时间进一步调查
    public class Container<T> implements Iterable<T> {
    
      // The capacity of the container.
      final int capacity;
      // The list.
      AtomicReference<Node<T>> head = new AtomicReference<Node<T>>();
    
      // Constructor
      public Container(int capacity) {
        this.capacity = capacity;
        // Construct the list.
        Node<T> h = new Node<T>();
        Node<T> it = h;
        // One created, now add (capacity - 1) more
        for (int i = 0; i < capacity - 1; i++) {
          // Add it.
          it.next = new Node<T>();
          // Step on to it.
          it = it.next;
        }
        // Make it a ring.
        it.next = h;
        // Install it.
        head.set(h);
      }
    
      // Empty ... NOT thread safe.
      public void clear() {
        Node<T> it = head.get();
        for (int i = 0; i < capacity; i++) {
          // Trash the element
          it.element = null;
          // Mark it free.
          it.free.set(true);
          it = it.next;
        }
        // Clear stats.
        resetStats();
      }
    
      // Add a new one.
      public Node<T> add(T element) {
        // Get a free node and attach the element.
        return getFree().attach(element);
      }
    
      // Find the next free element and mark it not free.
      private Node<T> getFree() {
        Node<T> freeNode = head.get();
        int skipped = 0;
        // Stop when we hit the end of the list 
        // ... or we successfully transit a node from free to not-free.
        while (skipped < capacity && !freeNode.free.compareAndSet(true, false)) {
          skipped += 1;
          freeNode = freeNode.next;
        }
        if (skipped < capacity) {
          // Put the head as next.
          // Doesn't matter if it fails. That would just mean someone else was doing the same.
          head.set(freeNode.next);
        } else {
          // We hit the end! No more free nodes.
          throw new IllegalStateException("Capacity exhausted.");
        }
        return freeNode;
      }
    
      // Mark it free.
      public void remove(Node<T> it, T element) {
        // Remove the element first.
        it.detach(element);
        // Mark it as free.
        if (!it.free.compareAndSet(false, true)) {
          throw new IllegalStateException("Freeing a freed node.");
        }
      }
    
      // The Node class. It is static so needs the <T> repeated.
      public static class Node<T> {
    
        // The element in the node.
        private T element;
        // Are we free?
        private AtomicBoolean free = new AtomicBoolean(true);
        // The next reference in whatever list I am in.
        private Node<T> next;
    
        // Construct a node of the list
        private Node() {
          // Start empty.
          element = null;
        }
    
        // Attach the element.
        public Node<T> attach(T element) {
          // Sanity check.
          if (this.element == null) {
            this.element = element;
          } else {
            throw new IllegalArgumentException("There is already an element attached.");
          }
          // Useful for chaining.
          return this;
        }
    
        // Detach the element.
        public Node<T> detach(T element) {
          // Sanity check.
          if (this.element == element) {
            this.element = null;
          } else {
            throw new IllegalArgumentException("Removal of wrong element.");
          }
          // Useful for chaining.
          return this;
        }
    
        @Override
        public String toString() {
          return element != null ? element.toString() : "null";
        }
      }
    
      // Provides an iterator across all items in the container.
      public Iterator<T> iterator() {
        return new UsedNodesIterator<T>(this);
      }
    
      // Iterates across used nodes.
      private static class UsedNodesIterator<T> implements Iterator<T> {
        // Where next to look for the next used node.
    
        Node<T> it;
        int limit = 0;
        T next = null;
    
        public UsedNodesIterator(Container<T> c) {
          // Snapshot the head node at this time.
          it = c.head.get();
          limit = c.capacity;
        }
    
        public boolean hasNext() {
          if (next == null) {
            // Scan to the next non-free node.
            while (limit > 0 && it.free.get() == true) {
              it = it.next;
              // Step down 1.
              limit -= 1;
            }
            if (limit != 0) {
              next = it.element;
            }
          }
          return next != null;
        }
    
        public T next() {
          T n = null;
          if ( hasNext () ) {
            // Give it to them.
            n = next;
            next = null;
            // Step forward.
            it = it.next;
            limit -= 1;
          } else {
            // Not there!!
            throw new NoSuchElementException ();
          }
          return n;
        }
    
        public void remove() {
          throw new UnsupportedOperationException("Not supported.");
        }
      }
    
      @Override
      public String toString() {
        StringBuilder s = new StringBuilder();
        Separator comma = new Separator(",");
        // Keep counts too.
        int usedCount = 0;
        int freeCount = 0;
        // I will iterate the list myself as I want to count free nodes too.
        Node<T> it = head.get();
        int count = 0;
        s.append("[");
        // Scan to the end.
        while (count < capacity) {
          // Is it in-use?
          if (it.free.get() == false) {
            // Grab its element.
            T e = it.element;
            // Is it null?
            if (e != null) {
              // Good element.
              s.append(comma.sep()).append(e.toString());
              // Count them.
              usedCount += 1;
            } else {
              // Probably became free while I was traversing.
              // Because the element is detached before the entry is marked free.
              freeCount += 1;
            }
          } else {
            // Free one.
            freeCount += 1;
          }
          // Next
          it = it.next;
          count += 1;
        }
        // Decorate with counts "]used+free".
        s.append("]").append(usedCount).append("+").append(freeCount);
        if (usedCount + freeCount != capacity) {
          // Perhaps something was added/freed while we were iterating.
          s.append("?");
        }
        return s.toString();
      }
    }
    
    private AtomicBoolean shutdown = new AtomicBoolean(); void shutdown() { shutdown.set(true); } while (!shutdown.get()) { if (source.hasNext()) { Object item = source.next(); while (!shutdown.get() && !queue.offer(item, 100, TimeUnit.MILLISECONDS)) { continue; } } else { break; } }