thread.interrupt()如何设置标志? [英] how does thread.interrupt() sets the flag?

查看:101
本文介绍了thread.interrupt()如何设置标志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档:

中断状态标志

The interrupt mechanism is implemented using an internal flag known as the interrupt status. Invoking Thread.interrupt sets this flag. When a thread checks for an interrupt by invoking the static method Thread.interrupted, interrupt status is cleared. The non-static isInterrupted method, which is used by one thread to query the interrupt status of another, does not change the interrupt status flag. By convention, any method that exits by throwing an InterruptedException clears interrupt status when it does so. However, it's always possible that interrupt status will immediately be set again, by another thread invoking interrupt.

The interrupt mechanism is implemented using an internal flag known as the interrupt status. Invoking Thread.interrupt sets this flag. When a thread checks for an interrupt by invoking the static method Thread.interrupted, interrupt status is cleared. The non-static isInterrupted method, which is used by one thread to query the interrupt status of another, does not change the interrupt status flag. By convention, any method that exits by throwing an InterruptedException clears interrupt status when it does so. However, it's always possible that interrupt status will immediately be set again, by another thread invoking interrupt.

我阅读了这些 SO 帖子.但是我认为我没有正确理解这一点.所以我在下面的示例代码上对此进行了测试. 我有两个threads在其中运行.一个是main,另一个是Thread-0,标记为t.

I read these SO posts. But I don't think I have understood this correctly. so I tested this on a sample code below. I have two threads running in this. one is main and other Thread-0 which is labeled t.

我调用t.interrupt(),然后在run方法中调用Thread.currentThread().isInterrupted(). 标志的确切位置在哪里? Thread Class是否维护当前正在运行的所有Threadsstatic list,并且通过调用Thread.currentThread()我得到了currentThread对象并检查它是否被中断了? 如果Thread类本身具有布尔标志interrupted,则如何区分两个线程;否则,如何区分两个线程?示例mainThread-0在这里. static flagThreads相同吗? 什么是确切的步骤顺序,set flaglook upunset flag等在哪里?

I call t.interrupt() and I call Thread.currentThread().isInterrupted() in the run method. Where exactly is the flag set? Does Thread Class maintains a static list of all the Threads that is currently running and by calling Thread.currentThread() I get the currentThread Object and check if it is interrupted? If the Thread class itself has the boolean flag interrupted, how does it differentiate between two threads; example main and Thread-0 here. Is the static flag same to both the Threads? what exact sequences of steps are happening, set flag, where is the look up and unset flag etc?

当我uncomment下面的code块(当前为c ommented out)时,该程序永不停止.我不明白为什么?

when I uncomment the block of code below (which is currently commented out), the program never stops. I do not understand why?

import java.util.Random;
    import java.util.concurrent.Callable;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Future;
    import java.util.concurrent.TimeUnit;

    public class ThreadInterrupt {

        public static void main(String[] args) throws InterruptedException {

            System.out.println("Starting.");

            Thread t  = new Thread(new Runnable(){

                @Override
                public void run() {
                    Random ran = new Random();

                    for (int i = 0; i < 1E8; i++) {

                        /*try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            System.out.println("we have been interrupted");
                            e.printStackTrace();
                        }*/

                        if (Thread.currentThread().isInterrupted()) {
                            System.out.println("Interrupted!");
                            break;
                    }

                    Math.sin(ran.nextDouble());
        }
            }
        });
        t.start();

        Thread.sleep(500);
        t.interrupt();
        t.join();

        System.out.println("Finished.");
    }

}

推荐答案

当我取消注释下面的代码块时(当前已注释) 退出),该程序永不停止.我不明白为什么?

when I uncomment the block of code below (which is currently commented out), the program never stops. I do not understand why?

请注意 Thread#interrupt()

如果在调用wait(),wait(long), 或Object类或join()的wait(long,int)方法, join(long),join(long,int), sleep(long)或sleep(long,int),方法 此类,然后将清除其中断状态并将其 收到InterruptedException.

If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.

因此,如果您取消注释代码,该标志将被取消,并且您的if将永远不会执行.

So if you uncomment your code, the flag gets lifted and your if never executes.

线程类维护所有线程的静态列表吗? 当前正在运行并通过调用Thread.currentThread()

Does Thread Class maintains a static list of all the Threads that is currently running and by calling Thread.currentThread()

Thread.currentThread() 方法声明为(Oracle JDK 7)

The Thread.currentThread() method is declared as (Oracle JDK 7)

public static native Thread currentThread();

换句话说,它是本地实现的,可能是用C代码实现的.给定javadoc,我们可以假设在某个地方存储了对所有线程的引用.返回当前正在执行的代码.

In other words, it is natively implemented, probably in C code. We can assume, given the javadoc, that, somewhere, a reference to all threads is stored. The currently executing one is returned.

类似地, Thread#isInterrupted() 方法调用

Similarly, the Thread#isInterrupted() method calls

private native boolean isInterrupted(boolean ClearInterrupted);

这也是本地实现的.但是我们可以假设它使用了一些布尔样式的标志.

which is also natively implemented. But we can assume it uses some boolean-style flag.

这篇关于thread.interrupt()如何设置标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆