java.util.concurrent


1. Creating and Using a Thread Pool

ExecutorService executorService = Executors.newFixedThreadPool(4);
executorService.submit(() -> {
    // Perform task
});

2. Executing Tasks Asynchronously

CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
    // Perform task
    return 10;
});
future.thenAccept(System.out::println);

3. Scheduling Tasks with ScheduledThreadPoolExecutor

ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(1);
scheduler.schedule(() -> {
    // Perform task
}, 5, TimeUnit.SECONDS);

4. Using a CountDownLatch to Wait for Multiple Tasks

CountDownLatch latch = new CountDownLatch(4);
ExecutorService executorService = Executors.newFixedThreadPool(4);

for (int i = 0; i < 4; i++) {
    executorService.submit(() -> {
        // Perform task
        latch.countDown();
    });
}

latch.await();

5. Using a Semaphore to Limit Concurrent Access

6. Using a Phaser to Synchronize Threads

7. Using BlockingQueue to Communicate Between Threads

8. Using CopyOnWriteArrayList for Concurrent Modification

9. Using ConcurrentHashMap for Concurrent Access to Maps

10. Using AtomicInteger for Concurrent Incrementation

11. Using AtomicReference for Concurrent Modification of Reference

12. Using Executors.newCachedThreadPool()

13. Using Executors.newSingleThreadExecutor()

14. Using CompletableFuture.allOf()

15. Using CompletableFuture.anyOf()

16. Using Executors.newWorkStealingPool()

17. Using Executors.newScheduledThreadPool()

18. Using Executors.newFixedThreadPool() with a Queue

19. Using Executors.newScheduledThreadPool() with a Fixed Delay

20. Using Executors.newSingleThreadScheduledExecutor()

21. Using CompletableFuture.thenAcceptBoth()

22. Using CompletableFuture.thenCombine()

23. Using CompletableFuture.thenCompose()

24. Using CompletableFuture.handle()

25. Using CompletableFuture.exceptionally()

26. Using CompletableFuture.applyToEither()

27. Using CompletableFuture.acceptEither()

28. Using Synchronous Blocking Queues

29. Using LinkedBlockingDeque

30. Using PriorityQueue

31. Using SynchronousQueue

32. Using ReentrantLock

33. Using Condition

34. Using Semaphore with Fairness

35. Using CountdownLatch with Timeouts

36. Using Phaser with Multiple Phases

37. Using StampedLock for Optimistic Concurrency

38. Using CyclicBarrier for Waiting on Multiple Threads

39. Using ThreadLocal for Thread-Local Storage

40. Using Executors.newWorkStealingPool(boolean)

41. Using Executors.newSingleThreadExecutor(ThreadFactory)

42. Using CompletableFuture.completeExceptionally()

43. Using CompletableFuture.thenRun()

44. Using CompletableFuture.orTimeout()

45. Using SynchronousQueue with Consumers

46. Using LinkedBlockingQueue with Iterators

47. Using PriorityQueue with Comparators

48. Using SynchronousQueue with Timeouts

49. Using ForkJoinPool

50. Using RecursiveTask