protected synchronized void startRunner() { stopRunner(); runner=timer_thread_factory != null? timer_thread_factory.newThread(this, "Timer runner") : new Thread(this, "Timer runner"); runner.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 synchronized void startRunner() { stopRunner(); runner=timer_thread_factory != null? timer_thread_factory.newThread(this, "Timer runner") : new Thread(this, "Timer runner"); runner.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(); }