1.shutdown方法:这个方法会平滑地关闭ExecutorService,当我们调用这个方法时,ExecutorService停止接受任何新的任务且等待已经提交的任务执行完成(已经提交的任务会分两类:一类是已经在执行的,另一类是还没有开始执行的),当所有已经提交的任务执行完毕后将会关闭ExecutorService。
2.awaitTermination方法:这个方法有两个参数,一个是timeout即超时时间,另一个是unit即时间单位。这个方法会使线程等待timeout时长,当超过timeout时间后,会监测ExecutorService是否已经关闭,若关闭则返回true,否则返回false。一般情况下会和shutdown方法组合使用。(awaitTermination不会关闭ExecutorService,只是定时检测一下他是否关闭)
public static void main(String[] args) throws IOException, InterruptedException {
ExecutorService service = Executors.newFixedThreadPool(3);
for (int i = 0; i < 4; i++) {
Runnable run = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("thread start");
}
};
service.execute(run);
}
service.shutdown();
while(!service.awaitTermination(2, TimeUnit.SECONDS)){
System.out.println("service not stop");
}
System.out.println("all thread complete");
}
运行结果:
service not stop
thread start
thread start
thread start
service not stop
service not stop
thread start
all thread complete
分享到:
相关推荐
不使用shutdown情况下系统关机 英文版系统下也可以用适合XPE系统
ShutDown命令 使用详细说明及各参数.docx
SHUTDOWN-定时开机关机 SHUTDOWN-定时开机关机 SHUTDOWN-定时开机关机 SHUTDOWN-定时开机关机 SHUTDOWN-定时开机关机
ShutDown Expert ShutDown Expert ShutDown Expert
循环使用最后两步,直到alter database open;后不再提示错误,出现“数据库已更改”。 然后接着输入即可 SQL>shutdown normal 提示:数据库已经关闭 已经卸载数据库 ORACLE 例程已经关闭 SQL>startup 提示:ORACLE...
此bat文件解决小米miui链接电脑进入fastboot模式时出现press any key to shutdown的问题,以管理员身份运行即可
列举讲解shutdown的具体应用 shutdown命令的相关参数
C#shutdown.rarC#shutdown.rarC#shutdown.rarC#shutdown.rar
部分手机在fastboot模式下在win10(8代CPU)上使用USB3.0接口连接会显示 press any key to shutdown解决方法: 下载修复文件到电脑,右键管理员运行后即可完美解决
shutdown.rar
2.shutdown how=SHUT_WR(关闭写端)可以保证对等方接收到一个EOF字符(FIN段),而不管是否有其他进程已经打开了套接字(shutdown并没采用引用计数)。 而close需要等待套接字引用计数减为0时才发送FIN段。也就是说...
shutdown自动关机很好用
Linux系统关机命令shutdown introduction
在oracle数据库中,关于shutdown命令的详解。对于新手很有帮助
ShutDown.rar 多条件设置定时关机,可设置cpu 使用情况定时关机 挂机者必备工具
PC Auto Shutdown是一款系统辅助软件,可以设置定时关机,重起,休眠,挂起和注销等。还可以在执行这些操作时做一些其他事情,如清除IE历史纪录,cookies,IE缓存文件等等。 它可以没有用户登录时,或当计算机是在...
电脑设置定时关机,shutdown命令详解
vista 关机命令 shutdownvista 关机命令 shutdownvista 关机命令 shutdownvista 关机命令 shutdownvista 关机命令 shutdown
ora-01033:oracle initialization or shutdown in progress 解决方法 ora-01033:oracle initialization or shutdown in progress 解决方法 ora-01033:oracle initialization or shutdown in progress 解决方法 ora-...
Shutdown Timer Classic 是一个小巧的 Windows 应用程序,它允许您设置一个计时器,该计时器将关闭,重新启动,休眠,睡眠或锁定您的计算机。它是完全免费和开源的,并且不使用互联网。随时为 GitHub 上的项目做贡献...