import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPoolExample {
public static void main(String[] args) {
// Creates a pool of 3 worker threads
ExecutorService executor = Executors.newFixedThreadPool(3);
for (int i = 1; i <= 5; i++) {
int taskNumber = i;
executor.submit(() -> {
System.out.println("Processing task " + taskNumber + " on " + Thread.currentThread().getName());
try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }
});
}
executor.shutdown(); // Stops accepting new tasks
}
}
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPoolExample {
public static void main(String[] args) {
// Creates a pool of 3 worker threads
ExecutorService executor = Executors.newFixedThreadPool(3);
}