Composable Concurrency in Java – Programming Language Guide

concurrencyjavalocksprogramming-languages

While I was reading a research paper on concurrency named Software and the Concurrency Revolution (html version). I came across following lines:

Unfortunately, although locks work, they pose serious problems for
modern software development. A fundamental problem with locks is that
they are not composable. You can’t take two correct lock-based pieces
of code, combine them, and know that the result is still correct.
Modern software development relies on the ability to compose libraries
into larger programs, and so it is a serious difficulty that we cannot
build on lock-based components without examining their
implementations.

  1. I was thinking, how Java guarantee composable concurrency or even there is a way to produce this scenarios.

  2. And how we can synchronize data in one or more libraries? Can a programmer do it from his program or it is up to library to synchronize things.

  3. If not Java then is there any other language which use lock based concurrency and guarantee composable concurrency?

Following is also taken from same paper:

There are at least three major problems with synchronized methods.
First, they are not appropriate for types whose methods call virtual
functions on other objects (e.g., Java’s Vector and .NET’s
SyncHashTable), because calling into third-party code while holding a
lock opens the possibility of deadlock
. Second, synchronized methods
can perform too much locking, by acquiring and releasing locks on all
object instances, even those never shared across threads (typically
the majority). Third, synchronized methods can also perform too
little locking, by not preserving atomicity when a program calls
multiple methods on an object or on different objects. As a simple
example of the latter, consider a banking transfer:
account1.Credit(amount); account2.Debit(amount)…

Note: Paper was published on September 2005

Best Answer

It's not the Java language. It's the nature of locks (mutexes).

There are better ways to get improved concurrency while still guaranteeing correctness, ways that are language independent:

  1. Using immutable objects, so that you don't need locks.
  2. Using promises and continuation-passing style.
  3. Using lock-free data structures.
  4. Using Software Transactional Memory to share state safely.

All of these techniques allow improved concurrency without using locks. None of them depend on the Java language specifically.

Related Topic