What is thread pool in java with example?



What is thread pool in java with example?..

Answer / Shivam Agarwal

A thread pool in Java is a collection of threads that are reused to handle multiple tasks efficiently. An ExecutorService, such as Executors.newFixedThreadPool(int n), creates a thread pool with a fixed number of threads. Here's an example:
```
ExecutorService executor = Executors.newFixedThreadPool(5);
for (int i = 0; i < 10; i++) {
final int taskId = i;
Runnable worker = new Runnable() {
public void run() {
System.out.println("Task " + taskId);
}
};
executor.execute(worker);
}
executor.shutdown();
```

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Core Java Interview Questions

What is the use of static import ?

4 Answers   Rolta,


What is an i/o filter?

6 Answers  


Any one can explain how the inerface uses in java. give with example.

1 Answers   IBM,


What are the Class Libraries ?

1 Answers   Wipro,


What is a java applet? What is an interface?

1 Answers  


Will the compiler creates a default constructor if I have a parameterized constructor in the class?

1 Answers  


Is string serializable in java?

1 Answers  


Convert a binary search tree to a sorted doubly linked list inplace.

1 Answers   Amazon,


What happens if an exception is not caught?

6 Answers   Amazon,


Write a program to check string is palindrome without using loop?

1 Answers   Cyient,


What is polymorphism in java? What are the kinds of polymorphism?

1 Answers  


Are there any tools available in java to create reports?

5 Answers  


Categories