Skip to main content

Java Concurrency

Ayman Patel

Ayman Patel

Back End Engineer @ Mastercard

Question in Discord: How do I synchronize threads? Java #concurrency Jan 18

Types of Thread Pools#

Link

FixedThreadPool#

Pool with 1 thread

CachedThreadPool#

Pool creates as many threads

ScheduledThreadPool#

SingleThreadedThreadPool#

Approaches for Thread syncing#

Use java.util.concurrent.CountDownLatch#

Use java.util.concurrent.ExecutorService.invokeAll()#

Use synchronized and volatle primities#

WARNING: For the LOVE OF GOD; DON'T DO THIS

Use java.util.concurrent.ForkJoinPool#

Use Message Passing constructs instead of Concurrency#

  • Actor Model
  • CSP (Communicationg Sequential Processes)
  • SEDA (Staged Event Driven Architecture)

Use java.util.concurrent.Exchanger#

Use java.util.concurrent.CyclicBarrier#

NOTE: Approach 2 seems to be very clean and a good higher-level abstraction.