1

我正在开发一个通过网络读取和处理数据的应用程序。在测试程序的连接/断开逻辑时,我注意到我的消费者线程在达到关闭条件时没有关闭。下面是消费者类的剥离版本。

import java.io.InputStream;

public class Consumer implements Runnable
{
   private final InputStream input;
   public Consumer(InputStream input)
   {
      this.input = input;
   }
   @Override
   public void run()
   {      
      byte readBuffer[];
      readBuffer = new byte[1];
      int goodData;

      try
      {
         while(input.available() > 0)
         {
            goodData = input.read(readBuffer);
            while (goodData > 0 )
            {
               System.out.println(readBuffer[0]);
               if ( readBuffer[0] == 27 )
               {
                  System.out.println("Consumer: found closing byte and closing thread "+Thread.currentThread().getName());
                  //this is the last packet, so interupt thread to close
              Thread.currentThread().interrupt();
              //return;
              //Thread.currentThread().stop(new InterruptedException("Attempting to close"));
               }
               goodData = input.read(readBuffer);
            }
         }
      }
      catch(Exception e)
      {
         System.out.println("closing "+Thread.currentThread().getName() +" because of an exception "+e.getClass());
         return;
      }
      System.out.println("closing "+Thread.currentThread().getName());
   }
}

我创建了一个演示该问题的虚拟主类。

public class ExampleOfInterruptNotWorking
{
   public static void main(String[] args)
   {
      byte[] bytesToWrite = new byte[]{0, 1, 2,3,4,5,6,65,23,65,21,54,13,54,1,76};
      Consumer C;
      Thread ConsumerThread;
      PipedInputStream PIS = null;
      PipedOutputStream POS = null;
      try
      {
         PIS = new PipedInputStream();
         POS = new PipedOutputStream(PIS);
         C = new Consumer(PIS);
         ConsumerThread = new Thread(C);

         ConsumerThread.start();

         POS.write(bytesToWrite);
         POS.write(bytesToWrite);
         bytesToWrite[1] = 27;
         POS.write(bytesToWrite);

         ConsumerThread.join();

      }
      catch(Exception e)
      {
         System.err.println("Unexpected exception in main");
         e.printStackTrace(System.err);
      }
      finally
      {
         try
         {
            PIS.close();
            POS.close();
         }
         catch(Exception ex)
         {
        //shouldn't happen in example
         }
         System.out.println("exiting main");
      }
   }
}

当您按照编写的方式运行此代码时,消费者会检测到中断,但不会停止执行,直到管道为空(不是我想要的)。只是为了尝试,我改为执行我想要的 Thread.stop() 调用,但我不想将其留在生产代码中。我意识到我可以使用一个简单的 return 语句,但这不是线程可以退出的唯一点,我希望有一些通用的退出代码来清理资源。所以,我的问题是,为什么消费者线程没有被中断?有没有让我能够拥有通用退出代码的好方法?

谢谢!

4

3 回答 3

9

当线程处于睡眠状态、等待连接等(基本上是任何可中断的阻塞调用)并调用 interrupt() 时,会抛出 InterruptedExceptions。

如果您的线程正在运行,则将设置线程中断标志但不会抛出异常,您应该使用 . 检查标志myThread.isInterrupted()

您可以在此处找到更多信息:http: //www.ibm.com/developerworks/java/library/j-jtp05236/index.html

于 2012-08-16T12:04:07.100 回答
2

您希望抛出哪种方法InterruptedExceptionThread.interrupt()不是扔它,也不是你的任何方法。那么你期望这个检查异常应该来自哪里?

您的代码不起作用,因为interrupt()几乎没有interrupted在线程上设置标志。您必须使用 . 显式检查该标志Thread.isInterrupted()InterruptedException仅当相关线程当时正在休眠或阻塞时才抛出。因此,如果您中断不同的线程并且该线程正在休眠,sleep()则会抛出InterruptedException.

现在详细解决您的问题。例外是针对特殊情况。您的线程完成处理的事实并非例外,这是您绝对期望的。出于同样的原因,在文件末尾读取文件不会引发异常 - 文件结束是您绝对应该期待的 - 所有文件都有结束。此外,您不应该使用异常来控制程序流程。

在您的情况下,要么使用return语句(run()返回时,线程死亡),要么以其他方式中断循环。您发布了太多无法分析的代码。

于 2012-08-16T12:02:28.290 回答
0

您可以简单地使用 break 来标记

OUTER:
     while(input.available() > 0)
     {
        goodData = input.read(readBuffer);
        while (goodData > 0 )
        {
           System.out.println(readBuffer[0]);
           if ( readBuffer[0] == 27 )
           {
              System.out.println("Consumer: found closing byte and closing thread "+Thread.currentThread().getName());
              //this is the last packet, so interupt thread to close
          //Thread.currentThread().interrupt();
          break OUTER;
          //return;
          //Thread.currentThread().stop(new InterruptedException("Attempting to close"));
           }
           goodData = input.read(readBuffer);
        }
     }
于 2012-08-17T09:51:01.347 回答