CodesJava

Easy learning with example program codes

Thread pool in java


Normally, server creates a new thread when a new request comes. This approach have several disadvantages. For creating a new thread for every new request will take more time and resources. Thread pool concept is introduced to resolve these problems.

Thread pool in java

A thread pool represents a group of threads which are waiting the job and can be used many times. In case of thread pool, when a new request comes then instead of creating a new thread, the job can be passed to thread pool.

Note: Thread pool helps us to limit the number of threads running in our application.

Example

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
public class ThreadPoolTest {
  public static void main(String[] args) {
	  ExecutorService executor = Executors.newFixedThreadPool(3);
      for (int i = 0; i < 5; i++) {  
          Runnable worker = new WorkerThread("" + i);  
          executor.execute(worker); 
        }  
      executor.shutdown();  
      while (!executor.isTerminated()) {   }  
 
      System.out.println("All threads are finished.");  
  }
}
 
class WorkerThread implements Runnable {  
    private String message;  
    public WorkerThread(String s){  
        this.message=s;  
    }  
     public void run() {  
        System.out.println(Thread.currentThread().getName()+" (Start) message = "+message);
        try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} 
        System.out.println(Thread.currentThread().getName()+" (End)"); 
    }   
}

Output

pool-1-thread-1 (Start) message = 0
pool-1-thread-3 (Start) message = 2
pool-1-thread-2 (Start) message = 1
pool-1-thread-1 (End)
pool-1-thread-1 (Start) message = 3
pool-1-thread-2 (End)
pool-1-thread-2 (Start) message = 4
pool-1-thread-3 (End)
pool-1-thread-1 (End)
pool-1-thread-2 (End)
All threads are finished.

Related topics

Posted in Java   

Core Java Tutorial

Programming language overview.
Overview of Java.
Java features
JVM architecture details.
JVM, JRE and JDK.
Java Coding Guidelines.
Some important definitions.
Variable and data types.
Hello world java program.
Core java examples programs.
Important Java Programs.
How to set permanent path in java?
OOPs Basics.
Object and Class in Java.
OOPs Principles/Concepts.
Abstraction in java.
Encapsulation in java.
Polymorphism in java.
Method overloading in java.
Method overriding in java.
Dynamic method dispatch.
Runtime polymorphism.
Association in java.
Inheritance in java.
Aggregation in java.
Command line arguments in java.
Command line argument program in eclipse.
Read input from command line using Scanner.
Java array programs
Java star pattern programs
Java number pattern programs
final in java.
Abstract class in java.
Interface in java.
Custom marker Interface in java.
Constructor in java.
Package in java.
Access modifier in java.
Static import in java.
Package class in java.
this in java.
Instance initialize block.
Anonymous block.
super in java.
Static in java.
final in java.
Java cloning deep and shallow
Shallow vs Clone copy
String handling in java.
String handling programs.
StringBuffer in java.
StringBuilder in java.
Exception Handling Tutorial.
Multithreading Tutoial.
Java input output stream tutorial.
Collections framework in java tutorial.
Collections framework programs.
Java Random class
Java annotations.
Java design principles.
Java 7 features
Java 8 features
Java networking tutorial
Java Reflection tutorial


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap