Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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 为什么Thread.interrupt()的行为是这样的?_Java_Multithreading - Fatal编程技术网

Java 为什么Thread.interrupt()的行为是这样的?

Java 为什么Thread.interrupt()的行为是这样的?,java,multithreading,Java,Multithreading,这是对Java并发教程中的代码的修改 package threads; public class SimpleThreads { static void threadMessage(String msg) { String threadName = Thread.currentThread().getName(); System.out.format("%s: %s%n", threadName,msg); } private static class MessageLoop i

这是对Java并发教程中的代码的修改

package threads;

public class SimpleThreads {
 static void threadMessage(String msg) {
  String threadName = Thread.currentThread().getName();
  System.out.format("%s: %s%n", threadName,msg);
 }
 private static class MessageLoop implements Runnable{

  @Override
  public void run() {
   // TODO Auto-generated method stub
   String[] importantInfo= {"apple","bat","chlorine","dog","egg","fog","gun"};
   try {
    for(int i=0;i<importantInfo.length;i++) {
     Thread.sleep(4000);
     threadMessage(importantInfo[i]);
    }
   }catch(InterruptedException ie) {
    threadMessage("i wasn't done");
   }
     }
 }
 /**
  * @param args
  */
 public static void main(String[] args) throws InterruptedException{
  // TODO Auto-generated method stub
  long patience = 100;
  if(args.length > 0)
   try {
    patience = Long.parseLong(args[0]) * 1000;
   }catch(NumberFormatException nfe) {
    System.err.println("argument must be a integer");
    System.exit(1);
   }
  threadMessage("starting message loop thread");
  long startTime = System.currentTimeMillis();
  Thread t = new Thread(new MessageLoop());
  t.start();

  threadMessage("waiting for messageloop thread to finish");
  while(t.isAlive()) {
   threadMessage("still waiting...");
   //t.join(1000);
   if(((System.currentTimeMillis() - startTime) > patience) && t.isAlive()) {
    threadMessage("tired of waiting");
    t.interrupt();
    //t.join();
   }
  }
  threadMessage("finally");
 }

}

我原以为在第一次(据说是唯一一次)
main:厌倦了等待
之后,我会看到
Thread-0:我还没有完成
,但是
main:厌倦了等待
出现了5次——这是为什么呢?

Thread.Interrupt()只是向目标线程发送一个中断(信号/异常);它不会立即杀死它

此外,在发送中断与目标线程接收和处理中断之间可能存在延迟;上下文切换不能保证是即时的

您可以通过执行某种形式的阻塞操作(如I/O或睡眠)或调用Thread.yield(),鼓励(但不是强制)JVM更快地切换上下文。

Thread.interrupt()
不会终止或中断线程。
它只是中断一些方法,如sleep、wait、join、可中断通道上的阻塞I/O或阻塞选择器。否则,它只设置线程的中断标志,由您来测试该标志。
如果在上面的代码中删除睡眠,中断将不会有明显的效果

线程将(可能)中断几次,因为实际线程(主循环)继续运行,直到系统更改为中断线程。中断后,您可以添加sleep()或yield()(如图所示,join()也可以工作)以允许中断的线程运行

    threadMessage("tired of waiting");
    t.interrupt();
    t.yield();

另请参见

main:厌倦等待
可能发生N次--5只是基于VM定时的任意数字。当然,因为上下文切换发生的时间也是(半)任意的。
    threadMessage("tired of waiting");
    t.interrupt();
    t.yield();