
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Thread Pools in Java
A thread pool is a collection of pre-initialized threads. The general plan behind a thread pool is to form variety of threads at method startup and place them into a pool, wherever they sit and expect work. once a server receives a call for participation, it awakens a thread from this pool—if one is available—and passes it the request for service. Once the thread completes its service, it returns to the pool and awaits a lot of work. If the pool contains no accessible thread, the server waits till one becomes free.
It saves time as a result of there's no need to produce new thread.
It is utilized in Servlet and JSP wherever instrumentality creates a thread pool to method the request.
Example
EmployeeThread.java
importjava.util.concurrent.ExecutorService; import java.util.concurrent.Executors; class EmployeeThread implements Runnable { private String message; public EmployeeThread(String s) { this.message=s; } public void run() { System.out.println(Thread.currentThread().getName()+" (Start) message = "+message); processmessage();//call processmessage method that sleeps the thread for 2 seconds System.out.println(Thread.currentThread().getName()+" (End)");//prints thread name } private void processmessage() { try { Thread.sleep(1000); } catch (InterruptedException e){ e.printStackTrace(); } } }
ExampleThreadPool.java
public class implementThreadPool { public static void main(String[] args) { ExecutorService executor = Executors.newFixedThreadPool(6); //creating a pool of 6 threads for (int m = 0; m< 6; m++) { Runnable worker = new EmployeeThread("" + i); executor.execute(worker); //calling execute method of ExecutorService } executor.shutdown(); while (!executor.isTerminated()) { } System.out.println("Finished all the threads"); } }
- Related Articles
- Thread Pools in C#
- User Thread vs Daemon Thread in Java?
- Daemon thread in Java
- How a thread can interrupt another thread in Java?
- Inter thread communication in Java
- Get current thread in Java
- Demonstrate thread priorities in Java
- Change Thread Priority in Java
- Naming a thread in Java
- Java Thread Priority in Multithreading
- Thread Interference Error in Java
- Is Java matcher thread safe in Java?
- What are Bitcoin Mining Pools?
- Controlling the main thread in Java
- Is Swing thread-safe in Java?

Advertisements