mayInterruptIfRunning设成false话,不允许在线程运行时中断,设成true的话就允许。 
可以参考下面的代码来理解,如果设为false的话,会打印到99999,如果设成true的话,可能就打印不到99999 

 

 

package interrupt;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Future01 {


    public static void main(String[] args) {
        ExecutorService eService = Executors.newFixedThreadPool(5);
        Future<?> future= eService.submit(new RunFuture());
        try {
            Thread.sleep(10);
            future.cancel(false);
            //future.cancel(true);
            System.out.println("haha---------------");
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }



}
class RunFuture implements Runnable{
    int i=0;
    @Override
    public void run() {
        while (i<100000&&!Thread.currentThread().isInterrupted()/**/) {
            System.out.println("i++:"+i);
            i++;
        }

    }

}

 

相关文章:

  • 2021-12-15
  • 2022-12-23
  • 2022-12-23
  • 2021-04-25
  • 2021-09-24
  • 2021-11-17
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-05-25
  • 2021-10-07
  • 2021-08-29
  • 2022-01-04
  • 2021-07-17
  • 2022-12-23
相关资源
相似解决方案