调度封装
public class SchedulerService extends Service { private static final int DEFAULT_POOL_SIZE = 2 * Runtime.getRuntime().availableProcessors(); private int poolSize; private ScheduledExecutorService pool; public int getPoolSize() { return poolSize; } public void setPoolSize(int poolSize) { if (poolSize < 0) { logger.warning("pool size error,use default value:" + DEFAULT_POOL_SIZE); poolSize = DEFAULT_POOL_SIZE; } this.poolSize = poolSize; } protected void initService() throws ServiceException { pool = Executors.newScheduledThreadPool(poolSize); } protected void startService() throws ServiceException { // } protected void stopService() throws ServiceException { pool.shutdown(); try { if (!pool.awaitTermination(60, TimeUnit.SECONDS)) { pool.shutdownNow(); if (!pool.awaitTermination(60, TimeUnit.SECONDS)) { logger.severe("Pool did not terminate"); } } } catch (InterruptedException e) { pool.shutdownNow(); Thread.currentThread().interrupt(); } } ScheduledExecutorService getPool() { return pool; } public Scheduler scheduleWithFixedDelay(Runnable task, long delay, long period, TimeUnit util) { Scheduler scheduler = new Scheduler(); scheduler.setDelay(delay); scheduler.setPeriod(period); scheduler.setWorker(task); scheduler.setService(this); return scheduler; } public Scheduler schedule(Runnable task, long delay, TimeUnit util) { Scheduler scheduler = new Scheduler(); scheduler.setDelay(delay); scheduler.setPeriod(-1); scheduler.setWorker(task); scheduler.setService(this); return scheduler; } }
public class Scheduler { private SchedulerService service; private Runnable worker; private long period = -1; private long delay = 0; private ScheduledFuture<?> task; void setService(SchedulerService service) { this.service = service; } void setWorker(Runnable worker) { this.worker = worker; } void setPeriod(long period) { this.period = period; } void setDelay(long delay) { this.delay = delay; } public SchedulerService getService() { return service; } public Runnable getWorker() { return worker; } public ScheduledFuture<?> getTask() { return task; } public void cancel() { if (this.task != null && (!this.task.isCancelled() || !this.task.isDone())) { this.task.cancel(true); } } public long getDelay() { return delay; } public long getPeriod() { return period; } public boolean isRunning() { return task != null && !task.isCancelled() && !task.isDone(); } public synchronized void start() { if (!isRunning()) { if (service != null && worker != null) { if (this.period > 0) { task = service.getPool().scheduleWithFixedDelay(worker, this.delay, this.period, TimeUnit.MILLISECONDS); } else { task = service.getPool().schedule(worker, this.delay, TimeUnit.MILLISECONDS); } } } } }
private Scheduler addSchedulerJob(final ITimeTask task,long delay ,final IPluginActivator activator) { Scheduler timer = schedulerService.scheduleWithFixedDelay(new Runnable() { public void run() { try { if (activator.getState() == IPluginActivator.RUNNING) { task.excute(); } } catch (Throwable e) { logger.log(Level.SEVERE, "", e); } } }, 0, delay, TimeUnit.MILLISECONDS); return timer; }
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。