public static Object[][] createTimer() { return new Object[][]{ {new TimeScheduler3()}, }; }
protected Future<?> doSchedule(Task task, long initial_delay) { if(task.getRunnable() == null) throw new NullPointerException(); if (isShutdown()) return null; if(initial_delay <= 0) { submitToPool(task); return task; } return add(task); }
public int getCurrentThreads() {return condGet(ThreadPoolExecutor::getPoolSize, 0);} public int getQueueSize() {return condGet(p -> p.getQueue().size(), 0);}
public void setMinThreads(int size) {condSet(p -> p.setCorePoolSize(size));} public int getMaxThreads() {return condGet(ThreadPoolExecutor::getMaximumPoolSize, 0);}
public TimeScheduler3(Executor thread_pool, ThreadFactory factory) { timer_thread_factory=factory; pool=thread_pool; start(); }
public Future<?> scheduleWithFixedDelay(Runnable work, long initial_delay, long delay, TimeUnit unit, boolean can_block) { return scheduleRecurring(work, TaskType.fixed_delay, initial_delay, delay, unit, can_block); }
public Future<?> schedule(Runnable work, long initial_delay, TimeUnit unit, boolean can_block) { return doSchedule(new Task(work, initial_delay, unit, can_block), initial_delay); }
protected Task add(Task task) { if(!isRunning()) return null; queue.add(task); return task; }
public TP setThreadPool(Executor thread_pool) { if(this.thread_pool != null) shutdownThreadPool(this.thread_pool); this.thread_pool=thread_pool; if(timer instanceof TimeScheduler3) ((TimeScheduler3)timer).setThreadPool(thread_pool); return this; }
public void run() { if(isDone()) return; super.run(); if(cancelled) return; done=false; // run again switch(type) { case dynamic: long next_interval=TimeUnit.NANOSECONDS.convert(((TimeScheduler.Task)runnable).nextInterval(), TimeUnit.MILLISECONDS); if(next_interval <= 0) { if(log.isTraceEnabled()) log.trace("task will not get rescheduled as interval is " + next_interval); done=true; return; } creation_time=System.nanoTime(); delay=next_interval; break; case fixed_rate: delay=initial_delay + cnt++ * period; break; case fixed_delay: creation_time=System.nanoTime(); delay=period; break; } add(this); // schedule this task again } }
public void setKeepAliveTime(long time) {condSet(p -> p.setKeepAliveTime(time, TimeUnit.MILLISECONDS));} public int getCurrentThreads() {return condGet(ThreadPoolExecutor::getPoolSize, 0);}
/** * Create a scheduler that executes tasks in dynamically adjustable intervals */ public TimeScheduler3() { pool=new ThreadPoolExecutor(4, 10, 30000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(100), Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy()); start(); }
public Future<?> scheduleAtFixedRate(Runnable work, long initial_delay, long delay, TimeUnit unit, boolean can_block) { return scheduleRecurring(work,TaskType.fixed_rate,initial_delay,delay,unit, can_block); }
protected Future<?> scheduleRecurring(Runnable work, TaskType type, long initial_delay, long delay, TimeUnit unit, boolean can_block) { return doSchedule(new RecurringTask(work, type, initial_delay, delay, unit, can_block), initial_delay); }
protected Task add(Task task) { if(!isRunning()) return null; queue.add(task); return task; }
public TP setThreadPool(Executor thread_pool) { if(this.thread_pool != null) shutdownThreadPool(this.thread_pool); this.thread_pool=thread_pool; if(timer instanceof TimeScheduler3) ((TimeScheduler3)timer).setThreadPool(thread_pool); return this; }
public void run() { if(isDone()) return; super.run(); if(cancelled) return; done=false; // run again switch(type) { case dynamic: long next_interval=TimeUnit.NANOSECONDS.convert(((TimeScheduler.Task)runnable).nextInterval(), TimeUnit.MILLISECONDS); if(next_interval <= 0) { if(log.isTraceEnabled()) log.trace("task will not get rescheduled as interval is " + next_interval); done=true; return; } creation_time=System.nanoTime(); delay=next_interval; break; case fixed_rate: delay=initial_delay + cnt++ * period; break; case fixed_delay: creation_time=System.nanoTime(); delay=period; break; } add(this); // schedule this task again } }
protected Future<?> doSchedule(Task task, long initial_delay) { if(task.getRunnable() == null) throw new NullPointerException(); if (isShutdown()) return null; if(initial_delay <= 0) { submitToPool(task); return task; } return add(task); }
public int getMinThreads() {return condGet(ThreadPoolExecutor::getCorePoolSize, 0);} public void setMinThreads(int size) {condSet(p -> p.setCorePoolSize(size));}
public void setMaxThreads(int size) {condSet(p -> p.setMaximumPoolSize(size));} public long getKeepAliveTime() {return condGet(p -> p.getKeepAliveTime(TimeUnit.MILLISECONDS), 0L);}