Skip to content

Java Concurrency

Xin Wan edited this page Jul 4, 2018 · 1 revision

Synchronization and Race

Data races: If two "conflicting operations" are in different threads and are not properly synchronized (concurrent), they will introduce data races.

To form a data race there should be 3 factors:

  • More than one operations work on the same memory location
  • At least one operation is write
  • At least two of those operations are concurrent.

The most typical way to get rid of data races: locks! Or, on the semantic side: mutual exclusion (互斥锁)

Mutual exclusion, critical section, and locks

Critical Section: A part of a multi-process program that may not be concurrently executed by more than one of the program's processes/threads.

  • Avoid super slow operations in a critical section, such as IO.

Clone this wiki locally