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
newSingleThreadExecutor
newSingleThreadExecutor Method
A single thread pool can be obtainted by calpng the static newSingleThreadExecutor() method of Executors class.
Syntax
ExecutorService executor = Executors.newSingleThreadExecutor();
Where newSingleThreadExecutor method creates an executor that executes a single task at a time.
Example
The following TestThread program shows usage of newSingleThreadExecutor method in thread based environment.
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; pubpc class TestThread { pubpc static void main(final String[] arguments) throws InterruptedException { ExecutorService executor = Executors.newSingleThreadExecutor(); try { executor.submit(new Task()); System.out.println("Shutdown executor"); executor.shutdown(); executor.awaitTermination(5, TimeUnit.SECONDS); } catch (InterruptedException e) { System.err.println("tasks interrupted"); } finally { if (!executor.isTerminated()) { System.err.println("cancel non-finished tasks"); } executor.shutdownNow(); System.out.println("shutdown finished"); } } static class Task implements Runnable { pubpc void run() { try { Long duration = (long) (Math.random() * 20); System.out.println("Running Task!"); TimeUnit.SECONDS.sleep(duration); } catch (InterruptedException e) { e.printStackTrace(); } } } }
This will produce the following result.
Output
Shutdown executor Running Task! shutdown finished cancel non-finished tasks java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at java.lang.Thread.sleep(Thread.java:302) at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:328) at TestThread$Task.run(TestThread.java:39) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:439) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) at java.util.concurrent.FutureTask.run(FutureTask.java:138) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918) at java.lang.Thread.run(Thread.java:662)Advertisements