protected void submitToPool(Task task) { if(non_blocking_task_handling && !task.canBlock()) { task.run(); return; } try { pool.execute(task); } catch(RejectedExecutionException rejected) { // only thrown if rejection policy is "abort" Thread thread=timer_thread_factory != null? timer_thread_factory.newThread(task, "Timer temp thread") : new Thread(task, "Timer temp thread"); thread.start(); } }
/** * Stops the timer, cancelling all tasks */ public void stop() { stopRunner(); // we may need to do multiple iterations as the iterator works on a copy and tasks might have been added just // after the iterator() call returned while(!queue.isEmpty()) for(Task entry: queue) { entry.cancel(true); queue.remove(entry); } queue.clear(); if(pool instanceof ThreadPoolExecutor) { ThreadPoolExecutor p=(ThreadPoolExecutor)pool; List<Runnable> remaining_tasks=p.shutdownNow(); remaining_tasks.stream().filter(task -> task instanceof Future).forEach(task -> ((Future)task).cancel(true)); p.getQueue().clear(); try { p.awaitTermination(Global.THREADPOOL_SHUTDOWN_WAIT_TIME, TimeUnit.MILLISECONDS); } catch(InterruptedException e) { } } // clears the threads list (https://issues.jboss.org/browse/JGRP-1971) if(timer_thread_factory instanceof LazyThreadFactory) ((LazyThreadFactory)timer_thread_factory).destroy(); }
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 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 } }
/** * Stops the timer, cancelling all tasks */ public void stop() { stopRunner(); // we may need to do multiple iterations as the iterator works on a copy and tasks might have been added just // after the iterator() call returned while(!queue.isEmpty()) for(Task entry: queue) { entry.cancel(true); queue.remove(entry); } queue.clear(); if(pool instanceof ThreadPoolExecutor) { ThreadPoolExecutor p=(ThreadPoolExecutor)pool; List<Runnable> remaining_tasks=p.shutdownNow(); remaining_tasks.stream().filter(task -> task instanceof Future).forEach(task -> ((Future)task).cancel(true)); p.getQueue().clear(); try { p.awaitTermination(Global.THREADPOOL_SHUTDOWN_WAIT_TIME, TimeUnit.MILLISECONDS); } catch(InterruptedException e) { } } // clears the threads list (https://issues.jboss.org/browse/JGRP-1971) if(timer_thread_factory instanceof LazyThreadFactory) ((LazyThreadFactory)timer_thread_factory).destroy(); }
protected void submitToPool(Task task) { if(non_blocking_task_handling && !task.canBlock()) { task.run(); return; } try { pool.execute(task); } catch(RejectedExecutionException rejected) { // only thrown if rejection policy is "abort" Thread thread=timer_thread_factory != null? timer_thread_factory.newThread(task, "Timer temp thread") : new Thread(task, "Timer temp thread"); thread.start(); } }
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); }
public void run() { if(isDone()) return; try { runnable.run(); } catch(Throwable t) { log.error(Util.getMessage("FailedExecutingTask") + ' ' + runnable, t); } finally { done=true; } }
public void execute(Runnable task, boolean can_block) { submitToPool(task instanceof TimeScheduler.Task? new RecurringTask(task, TaskType.dynamic, 0, ((TimeScheduler.Task)task).nextInterval(), TimeUnit.MILLISECONDS, can_block) : new Task(task, can_block)); // we'll execute the task directly }
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 boolean cancel(boolean mayInterruptIfRunning) { boolean retval=!isDone(); cancelled=true; return retval; }
public int compareTo(Delayed o) { long my_delay=getDelay(TimeUnit.NANOSECONDS), other_delay=o.getDelay(TimeUnit.NANOSECONDS); // return Long.compare(my_delay, other_delay); // JDK 7 only return (my_delay < other_delay) ? -1 : ((my_delay == other_delay) ? 0 : 1); }
public String dumpTimerTasks() { StringBuilder sb=new StringBuilder(); for(Task task: queue) { sb.append(task); if(task.isCancelled()) sb.append(" (cancelled)"); sb.append("\n"); } return sb.toString(); }
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 Future<?> schedule(Runnable work, long initial_delay, TimeUnit unit, boolean can_block) { return doSchedule(new Task(work, initial_delay, unit, can_block), initial_delay); }
public void run() { if(isDone()) return; try { runnable.run(); } catch(Throwable t) { log.error(Util.getMessage("FailedExecutingTask") + ' ' + runnable, t); } finally { done=true; } }
public void execute(Runnable task, boolean can_block) { submitToPool(task instanceof TimeScheduler.Task? new RecurringTask(task, TaskType.dynamic, 0, ((TimeScheduler.Task)task).nextInterval(), TimeUnit.MILLISECONDS, can_block) : new Task(task, can_block)); // we'll execute the task directly }
public int compareTo(Delayed o) { long my_delay=getDelay(TimeUnit.NANOSECONDS), other_delay=o.getDelay(TimeUnit.NANOSECONDS); // return Long.compare(my_delay, other_delay); // JDK 7 only return (my_delay < other_delay) ? -1 : ((my_delay == other_delay) ? 0 : 1); }
public String dumpTimerTasks() { StringBuilder sb=new StringBuilder(); for(Task task: queue) { sb.append(task); if(task.isCancelled()) sb.append(" (cancelled)"); sb.append("\n"); } return sb.toString(); }
public boolean cancel(boolean mayInterruptIfRunning) { boolean retval=!isDone(); cancelled=true; return retval; }