packageuu;importjava.util.concurrent.Executors;importjava.util.concurrent.Future;importjava.util.concurrent.ScheduledExecutorService;importjava.util.concurrent.ScheduledFuture;importjava.util.concurrent.ThreadFactory;importjava.util.concurrent.TimeUnit;importjava.util.concurrent.atomic.AtomicInteger;/*** 任务调度工具类
*
*@authorlinxu
* @date 2020/07/18
**/
public classExecutorUtil {public static final ScheduledExecutorService COMMON_LOGIC_EXECUTOR = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors() * 2, newThreadFactory() {
AtomicInteger count= new AtomicInteger(0);
@OverridepublicThread newThread(Runnable r) {int curCount =count.incrementAndGet();
System.err.println("任务调度线程池"+curCount);return new Thread(r, "任务调度线程池" +curCount);
}
});public static ScheduledFuture> scheduleAtFixedRate(Runnable command, long initialDelay, longperiod) {returnCOMMON_LOGIC_EXECUTOR.scheduleAtFixedRate(command, initialDelay, period, TimeUnit.MILLISECONDS);
}public static ScheduledFuture> scheduleAtFixedRate(Runnable command, long initialDelay, longperiod,
TimeUnit unit) {returnCOMMON_LOGIC_EXECUTOR.scheduleAtFixedRate(command, initialDelay, period, unit);
}public static ScheduledFuture> scheduleWithFixedDelay(Runnable command, long initialDelay, longperiod,
TimeUnit unit) {returnCOMMON_LOGIC_EXECUTOR.scheduleWithFixedDelay(command, initialDelay, period, unit);
}//延时执行,不重复
public static ScheduledFuture> schedule(Runnable command, longdelay, TimeUnit unit) {returnCOMMON_LOGIC_EXECUTOR.schedule(command, delay, unit);
}public static Future>submit(Runnable task) {returnCOMMON_LOGIC_EXECUTOR.submit(task);
}//优雅的关闭线程池
public static voidclose() {
COMMON_LOGIC_EXECUTOR.shutdown();try{// //等待未完成任务结束
System.err.println("等待未完成任务结束");if (!COMMON_LOGIC_EXECUTOR.awaitTermination(60, TimeUnit.SECONDS)) {
COMMON_LOGIC_EXECUTOR.shutdown();
}// //等待任务取消的响应
System.err.println("等待任务取消的响应");if (!COMMON_LOGIC_EXECUTOR.awaitTermination(60, TimeUnit.SECONDS)) {
}
}catch(Exception e) {
COMMON_LOGIC_EXECUTOR.shutdown();//保留中断状态
System.err.println("保留中断状态");
Thread.currentThread().interrupt();
}
}public static void main(String[] args) throwsException {
System.err.println("jvm空闲内存:"+Runtime.getRuntime().freeMemory());
System.err.println("jvm最大内存:"+Runtime.getRuntime().maxMemory());
System.err.println("jvm总内存:"+Runtime.getRuntime().totalMemory());final ScheduledFuture> scheduledFuture = schedule(newRunnable() {
@Overridepublic voidrun() {//TODO Auto-generated method stub
System.err.println("sdmnclksdklds1");
}
},3000, TimeUnit.MILLISECONDS);
schedule(newRunnable() {
@Overridepublic voidrun() {//TODO Auto-generated method stub
System.err.println("sdmnclksdklds2");
}
},1000, TimeUnit.MILLISECONDS);newThread() {public voidrun() {try{
Thread.sleep(1000);
System.err.println("是否完成:"+scheduledFuture.isDone());
}catch(InterruptedException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();newThread() {public voidrun() {try{
Thread.sleep(5000);
System.err.println("是否完成:"+scheduledFuture.isDone());
close();
}catch(InterruptedException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
}