Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package multithreading.thread_pool.types;
- import java.util.HashSet;
- import java.util.Set;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.TimeUnit;
- public class FixedThreadPoolClient {
- private static final int TASKS_COUNT = 10_000;
- private static final Set<String> threadNames = new HashSet<>();
- public static void main(String[] args) throws InterruptedException {
- ExecutorService service = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
- for (int i = 0; i < TASKS_COUNT; i++) {
- int finalI = i;
- service.execute(() -> {
- System.out.println("Thread " + Thread.currentThread().getName() + " executes " + finalI + " task");
- synchronized (threadNames) {
- threadNames.add(Thread.currentThread().getName());
- }
- });
- }
- service.shutdown();
- service.awaitTermination(30, TimeUnit.SECONDS);
- System.out.println("Count of used threads " + threadNames.size());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement