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.*;
- public class ScheduledThreadPoolClient {
- private static final int TASKS_COUNT = 10_000;
- private static final Set<String> threadNames = new HashSet<>();
- public static void main(String[] args) throws InterruptedException {
- ScheduledExecutorService service = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors());
- for (int i = 0; i < TASKS_COUNT; i++) {
- int finalI = i;
- Runnable task = () -> {
- System.out.println("Thread " + Thread.currentThread().getName() + " executes " + finalI + " task");
- synchronized (threadNames) {
- threadNames.add(Thread.currentThread().getName());
- }
- };
- service.schedule(task, 1, TimeUnit.SECONDS);
- }
- 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