Java Concurrency Tutorial
Selected Reading
- 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
Concurrency - Executor
Java Concurrency - Executor Interface
A java.util.concurrent.Executor interface is a simple interface to support launching new tasks.
ExecutorService Methods
Sr.No. | Method & Description |
---|---|
1 | void execute(Runnable command) Executes the given command at some time in the future. |
Example
The following TestThread program shows usage of Executor interface in thread based environment.
import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; pubpc class TestThread { pubpc static void main(final String[] arguments) throws InterruptedException { Executor executor = Executors.newCachedThreadPool(); executor.execute(new Task()); ThreadPoolExecutor pool = (ThreadPoolExecutor)executor; pool.shutdown(); } static class Task implements Runnable { pubpc void run() { try { Long duration = (long) (Math.random() * 5); System.out.println("Running Task!"); TimeUnit.SECONDS.sleep(duration); System.out.println("Task Completed"); } catch (InterruptedException e) { e.printStackTrace(); } } } }
This will produce the following result.
Output
Running Task! Task CompletedAdvertisements