Last active
August 1, 2019 01:36
-
-
Save vector4wang/08b3399f53f511b72fa45631c957c1c7 to your computer and use it in GitHub Desktop.
[Java ExecutorService 运行指定时间后停止] #Java #线程
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ExecutorService es = Executors.newFixedThreadPool(10); | |
es.execute(new Thread());//执行子线程任务 | |
try { | |
es.shutdown(); | |
if(!es.awaitTermination(20,TimeUnit.SECONDS)){//20S | |
System.out.println(" 到达指定时间,还有线程没执行完,不再等待,关闭线程池!"); | |
es.shutdownNow(); | |
} | |
} catch (Throwable e) { | |
// TODO Auto-generated catch block | |
es.shutdownNow(); | |
e.printStackTrace(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment