- Concurrency - Discussion
- Concurrency - Useful Resources
- Concurrency - Quick Guide
- ConcurrentNavigableMap
- Concurrency - ConcurrentMap
- Concurrency - BlockingQueue
- Concurrency - Fork-Join framework
- Concurrency - Futures and Callables
- ScheduledThreadPoolExecutor
- Concurrency - ThreadPoolExecutor
- newSingleThreadExecutor
- newScheduledThreadPool
- Concurrency - newCachedThreadPool
- Concurrency - newFixedThreadPool
- ScheduledExecutorService
- Concurrency - ExecutorService
- Concurrency - Executor
- Concurrency - AtomicReferenceArray
- Concurrency - AtomicLongArray
- Concurrency - AtomicIntegerArray
- Concurrency - AtomicReference
- Concurrency - AtomicBoolean
- Concurrency - AtomicLong
- Concurrency - AtomicInteger
- Concurrency - Condition
- Concurrency - ReadWriteLock
- Concurrency - Lock
- Concurrency - ThreadLocalRandom
- Concurrency - ThreadLocal
- Concurrency - Deadlock
- Concurrency - Synchronization
- Interthread Communication
- Concurrency - Major Operations
- Concurrency - Environment Setup
- Concurrency - Overview
- Concurrency - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
ScheduledExecutorService Interface
A java.util.concurrent.ScheduledExecutorService interface is a subinterface of ExecutorService interface, and supports future and/or periodic execution of tasks.
ScheduledExecutorService Methods
Sr.No. | Method & Description |
---|---|
1 | <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) Creates and executes a ScheduledFuture that becomes enabled after the given delay. |
2 | ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) Creates and executes a one-shot action that becomes enabled after the given delay. |
3 | ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; that is executions will commence after initialDelay then initialDelay+period, then initialDelay + 2 * period, and so on. |
4 | ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given delay between the termination of one execution and the commencement of the next. |
Example
The following TestThread program shows usage of ScheduledExecutorService interface in thread based environment.
import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; pubpc class TestThread { pubpc static void main(final String[] arguments) throws InterruptedException { final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); final ScheduledFuture<?> beepHandler = scheduler.scheduleAtFixedRate(new BeepTask(), 2, 2, TimeUnit.SECONDS); scheduler.schedule(new Runnable() { @Override pubpc void run() { beepHandler.cancel(true); scheduler.shutdown(); } }, 10, TimeUnit.SECONDS); } static class BeepTask implements Runnable { pubpc void run() { System.out.println("beep"); } } }
This will produce the following result.
Output
beep beep beep beepAdvertisements