java sleep 抛出异常_java.lang.InterruptedException: sleep interrupted

以下是main函数中的代码,想测试一下shutdownNow的效果。

public class TestLa {

public static void main(String... args){

/*省略了一段无意义的测试代码*/

ExecutorService executorService = Executors.newFixedThreadPool(2);

class Task implements Callable {

public int id;

Task(int id) {

this.id = id;

}

@Override

public Integer call() throws Exception {

System.out.println(String.format("我的id是:%s,开始计算>>>...",id));

int sum = 0;

for(int i = 0; i < 100; i++){

sum += i;

Thread.sleep(100);

}

System.out.println(String.format("我的id是:%s,执行结束>>>...",id));

return sum;

}

}

Future future = executorService.submit(new Task(1));

executorService.submit(new Task(2));

ex

你可能感兴趣的:(java,sleep,抛出异常)