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 - newFixedThreadPool
newFixedThreadPool Method
A fixed thread pool can be obtainted by calpng the static newFixedThreadPool() method of Executors class.
Syntax
ExecutorService fixedPool = Executors.newFixedThreadPool(2);
where
Maximum 2 threads will be active to process tasks.
If more than 2 threads are submitted then they are held in a queue until threads become available.
A new thread is created to take its place if a thread terminates due to failure during execution shutdown on executor is not yet called.
Any thread exists till the pool is shutdown.
Example
The following TestThread program shows usage of newFixedThreadPool method in thread based environment.
import java.util.concurrent.ExecutorService; 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 { ExecutorService executor = Executors.newFixedThreadPool(2); // Cast the object to its class type ThreadPoolExecutor pool = (ThreadPoolExecutor) executor; //Stats before tasks execution System.out.println("Largest executions: " + pool.getLargestPoolSize()); System.out.println("Maximum allowed threads: " + pool.getMaximumPoolSize()); System.out.println("Current threads in pool: " + pool.getPoolSize()); System.out.println("Currently executing threads: " + pool.getActiveCount()); System.out.println("Total number of threads(ever scheduled): " + pool.getTaskCount()); executor.submit(new Task()); executor.submit(new Task()); //Stats after tasks execution System.out.println("Core threads: " + pool.getCorePoolSize()); System.out.println("Largest executions: " + pool.getLargestPoolSize()); System.out.println("Maximum allowed threads: " + pool.getMaximumPoolSize()); System.out.println("Current threads in pool: " + pool.getPoolSize()); System.out.println("Currently executing threads: " + pool.getActiveCount()); System.out.println("Total number of threads(ever scheduled): " + pool.getTaskCount()); executor.shutdown(); } static class Task implements Runnable { pubpc void run() { try { Long duration = (long) (Math.random() * 5); System.out.println("Running Task! Thread Name: " + Thread.currentThread().getName()); TimeUnit.SECONDS.sleep(duration); System.out.println("Task Completed! Thread Name: " + Thread.currentThread().getName()); } catch (InterruptedException e) { e.printStackTrace(); } } } }
This will produce the following result.
Output
Largest executions: 0 Maximum allowed threads: 2 Current threads in pool: 0 Currently executing threads: 0 Total number of threads(ever scheduled): 0 Core threads: 2 Largest executions: 2 Maximum allowed threads: 2 Current threads in pool: 2 Currently executing threads: 1 Total number of threads(ever scheduled): 2 Running Task! Thread Name: pool-1-thread-1 Running Task! Thread Name: pool-1-thread-2 Task Completed! Thread Name: pool-1-thread-2 Task Completed! Thread Name: pool-1-thread-1Advertisements