Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java线程和中断,强大的拼图_Java - Fatal编程技术网

Java线程和中断,强大的拼图

Java线程和中断,强大的拼图,java,Java,代码涉及Java线程和中断()方法。您能告诉我,它是如何通过多次运行同一代码获得意外结果的吗?为什么这些结果不是唯一的。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。谢谢大家! 您的代码具有并发行为,这意味着每个线程同时运行,您将无法判断哪个线程启动,哪个线程停止。如果您不清楚我的难题,请放心!让我补充一下!☺请注意,有一个线程包装了主< /代码> Meo,因此您有不止一个线程

代码涉及Java线程和中断()方法。您能告诉我,它是如何通过多次运行同一代码获得意外结果的吗?为什么这些结果不是唯一的。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。谢谢大家!

您的代码具有并发行为,这意味着每个线程同时运行,您将无法判断哪个线程启动,哪个线程停止。

如果您不清楚我的难题,请放心!让我补充一下!☺请注意,有一个线程包装了<代码>主< /代码> Meo,因此您有不止一个线程不理解您的意思,我只是在调用中断()之后连续使用三次T.ISBuffTD(),它应该给我三个相同的结果。您无法知道threzd何时被中断,请记住线程中的指令不再是顺序的
public class InterruptTest extends Thread
{
   public void run() 
   {
     try {
        Thread.sleep(1000);
        System.out.println("Finished!");
      } 
     catch (InterruptedException e) {
        System.out.println("I am interrupted!");
        System.out.println(Thread.interrupted());// showing the result after exception throw as well as flag set to false
        System.exit(-1);
      }
   }
   public static void main(String args[])
   {
        InterruptTest t1 = new InterruptTest();
        Thread t= new Thread(t1);

        t.start();
        System.out.println(t.isInterrupted());// ask for the judgment in first time before calling interrupt().

        t.interrupt();
        System.out.println(t.isInterrupted());// ask for the judgment in second time after calling interrupt().
        System.out.println(t.isInterrupted());// ask for the judgment in third time after calling interrupt(). Very Puzzled!!!
        System.out.println(t.isInterrupted());// ask for the judgment in fourth time after calling interrupt().
   }
}

Console Display:

false// sure 
true// not sure 
true// not sure
true// not sure
I am interrupted!// sure
false// sure

false// sure 
true// not sure
true// not sure
false// not sure
I am interrupted!// sure 
false// sure 

false// sure 
true// not sure
false// not sure
false// not sure
I am interrupted!// sure 
false// sure