Java 从executorService接口立即关闭isn';t正在关闭进程中的任务
我已经使用了shutdownNow在第一个进程从invokeAny方法给出一些输出后立即关闭这些进程。但在输出中,我可以看到,即使在调用shutDownNow()之后,进程也处于完成流中,完成了它们的工作,然后才关闭。 代码:Java 从executorService接口立即关闭isn';t正在关闭进程中的任务,java,multithreading,concurrency,executorservice,shutdown,Java,Multithreading,Concurrency,Executorservice,Shutdown,我已经使用了shutdownNow在第一个进程从invokeAny方法给出一些输出后立即关闭这些进程。但在输出中,我可以看到,即使在调用shutDownNow()之后,进程也处于完成流中,完成了它们的工作,然后才关闭。 代码: int numberofrecordsinserted成功=0; List userRecordList=readFile( “C:\\Users\\sonar\\git\\multi-threading-Cocurrency\\JavaSEConcurrencyAPIS
int numberofrecordsinserted成功=0;
List userRecordList=readFile(
“C:\\Users\\sonar\\git\\multi-threading-Cocurrency\\JavaSEConcurrencyAPIStudyProject\\src\\main\\java\\Resources\\ExecutorServiceUserFile.txt”);
//ExecutorService ExecutorService=Executors.newSingleThreadExecutor()//单个线程的线程池。
//ExecutorService ExecutorService=Executors.newFixedThreadPool(3)//这里的线程池大小是3
ExecutorService ExecutorService=Executors.newCachedThreadPool();
UserDao UserDao=newuserdao();
List LISTOFCALABLE=new ArrayList();
forEach(x->listOfCallable.add(newuserprocessor(x,userDao));
试一试{
整数future=executorService.invokeAny(listOfCallable);
System.out.println(未来);
}捕获(中断异常|执行异常e){
e、 printStackTrace();
}
System.out.println(executorService.shutdownNow());
System.out.println(“ExecutorService正在关闭”+ExecutorService.isShutdown())//在获得第一个未来结果后,将执行此语句。
System.out.println(“ExecutorService已终止”+ExecutorService.isTerminated());
控制台上的输出为:
有关代码的更多详细信息,请参阅上面的git链接
类:ExecutorServiceThreeTypesOfShutdownMethodMain类
包:com.Concurrency.JavaSEConcurrencyAPIStudyProject.highlevelapi.ExecutorServiceInterface
要使用的项目:JavaSEConcurrencyAPIStudyProject
git链接:
请帮忙,为什么会这样?如何解决此问题?该方法的Javadoc说明如下: 除了尽最大努力尝试停止处理积极执行的任务之外,没有其他保证。例如,典型的实现将通过Thread.interrupt()取消,因此任何未能响应中断的任务可能永远不会终止
因此,如果您的任务不检查中断标志并捕获并忽略
InterruptedException
,则shutdownow()
将无法停止它们@PriyankaWagh-解决方案是更改任务代码,以便它检查中断并正确处理InterruptedException
。(这包括任务代码调用的任何第三方库。)@Ivan非常感谢。我希望您分享的观点是正确的,但在这种情况下,我觉得使用ShutdownNow()而不是Shutdown()来立即停止进程没有任何意义。@Stephen C您能建议代码需要做哪些更改吗?Bcos我是多线程和并发方面的新手,我想知道这一点。从阅读以下内容开始:(您向我们展示的代码中不需要更改代码……因此您必须自己解决它。阅读教程后。)
int numberOfRecordsInsertedSuccessfully = 0;
List<String> userRecordList = readFile(
"C:\\Users\\sonar\\git\\MultiThreading-Cocurrency\\JavaSEConcurrencyAPIStudyProject\\src\\main\\java\\Resources\\ExecutorServiceUserFile.txt");
// ExecutorService executorService = Executors.newSingleThreadExecutor(); //Thread pool of single thread.
// ExecutorService executorService = Executors.newFixedThreadPool(3); //Thread pool size is 3 here
ExecutorService executorService = Executors.newCachedThreadPool();
UserDao userDao = new UserDao();
List<Callable<Integer>> listOfCallable= new ArrayList<>();
userRecordList.forEach(x -> listOfCallable.add(new UserProcessor(x, userDao)));
try {
Integer future = executorService.invokeAny(listOfCallable);
System.out.println(future);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
System.out.println(executorService.shutdownNow());
System.out.println("ExecutorService is shutting down " + executorService.isShutdown()); //After getting first future result this statement will be executed.
System.out.println("ExecutorService is Terminated " + executorService.isTerminated());