Java Thread.isInterrupted不';不工作,线程被中断

Java Thread.isInterrupted不';不工作,线程被中断,java,multithreading,interrupted-exception,Java,Multithreading,Interrupted Exception,下面的程序演示了这个问题(最新的JVM&whatnot): publicstaticvoidmain(String[]args)抛出InterruptedException{ //如果这是真的,中断和中断的工作 最终布尔值withPrint=false; //决定是使用“中断”还是“中断”。 //如果这是真的,程序永远不会终止。 最终布尔值useIsInterrupted=true; ExecutorService executor=Executors.newSingleThreadExecut

下面的程序演示了这个问题(最新的JVM&whatnot):

publicstaticvoidmain(String[]args)抛出InterruptedException{
//如果这是真的,中断和中断的工作
最终布尔值withPrint=false;
//决定是使用“中断”还是“中断”。
//如果这是真的,程序永远不会终止。
最终布尔值useIsInterrupted=true;
ExecutorService executor=Executors.newSingleThreadExecutor();
最终倒计时闩锁=新倒计时闩锁(1);
Callable Callable=new Callable(){
@凌驾
public Void call()引发异常{
随机=新随机();
while(true){
如果(打印){
System.out.println(random.nextInt());
System.out.flush();
}
如果(使用中断)
{
如果(Thread.currentThread().isInterrupted())
打破
}
其他的
{
if(Thread.interrupted())
打破
}
}
System.out.println(“良好关机”);
倒计时();
返回null;
}
};
System.out.println(“任务提交”);
未来任务=执行者提交(可调用);
睡眠(100);
任务。取消(true);
satch.wait();
System.out.println(“主退出”);
executor.shutdown();
}

裂土器234,我刚刚在我的机器上运行了它,它总是停止,无论我对打印使用什么值以及使用什么中断。我使用的是jdk1.6.0_16。看看javadoc,它可能与interrupted()在每次调用后清除(interrupted)状态,而isInterrupted()不清除有关。事实上,它有时对jerome有效,总是对我有效,而从来没有对你有效,这可能表明我们正在使用的JDK或我们机器的速度有所不同。如果这与清除状态有关,那么这可能解释了可变性。

这看起来像是多处理器机器的一个问题,主要是在64位操作系统和1.5-7.0版本的java中

问题描述: 同时运行两个线程时,第一个线程使用thread.interrupt()中断第二个线程。 第二个线程测试调用thread.isInterrupted()方法是否被中断,该方法始终返回false

这发生在运行64位操作系统(Vista和Linux)的多处理器PC上。 在Vista 64位上,当使用64位JVM(从1.5到1.7的所有版本)时会发生这种情况,但在使用32位JVM时不会发生这种情况。 在Linux 64位上,使用64位JVM(1.5到1.7的所有版本)或32位JVM(1.5到1.7的所有版本)时会发生这种情况


解决方案是安装带有修复程序的版本,即1.6.0_16-b02或更高版本。

它并不总是无法停止。jerome,请检查编译器设置。。。可能您使用的是早期的JVM.Nice catch,但是bug的状态是“在hs16(b04)中已解决已修复”。这是我正在运行的版本:java版本“1.6.0_14”java(TM)SE运行时环境(build 1.6.0_14-b08)java HotSpot(TM)64位服务器VM(build 14.0-b16,混合模式),或者我不理解java版本号。这个bug应该在我的JDK中修复吗?我应该重新打开它还是下载一个新的JDK?hs16(b04)是热点版本,而不是JRE版本。看起来修复程序出现在1.6.0_16-b02中,该版本比您的版本晚,因此您应该下载最新版本(1.6.0_17)。我转而使用原子布尔函数来向线程发送信号,而不是中断线程。还没有检查更新JVM是否解决了这个问题。
public static void main(String[] args) throws InterruptedException {
    // if this is true, both interrupted and isInterrupted work
    final boolean withPrint = false;

    // decide whether to use isInterrupted or interrupted.
    // if this is true, the program never terminates.
    final boolean useIsInterrupted = true;

    ExecutorService executor = Executors.newSingleThreadExecutor();
    final CountDownLatch latch = new CountDownLatch(1);
    Callable<Void> callable = new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            Random random = new Random();
            while (true) {
                if (withPrint) {
                    System.out.println(random.nextInt());
                    System.out.flush();
                }
                if (useIsInterrupted)
                {
                    if (Thread.currentThread().isInterrupted())
                        break;
                }
                else
                {
                    if (Thread.interrupted())
                        break;
                }
            }
            System.out.println("Nice shutdown");
            latch.countDown();
            return null;
        }
    };
    System.out.println("Task submitted");
    Future<Void> task = executor.submit(callable);
    Thread.sleep(100);
    task.cancel(true);
    latch.await();
    System.out.println("Main exited");
    executor.shutdown();
}