• Post Reply Bookmark Topic Watch Topic
  • New Topic

Learning multithreading and Concurrency through examples of buggy code

 
Benjamin Bh Weaver
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In an interview I was recently asked a question about what bugs might be caused by a certain patch of code. This code involved the increment(++) of a volatile int variable. I learned a lot from looking at the question and then being reminded that the increment of that variable consists of not 1 but 2 processes--read and write--which, though individually atomic, can lead to race conditions in their combination.

Whether or not I understand the above example correctly, this got me to thinking: while there are good books about writing proper current code (e.g. B. Goetz, Concurrency in Practice), I wondered:

is there available anywhere a list of examples of buggy multithreaded code, like the example above, with explanations of the problems?

Multithreaded bugs are often described as subtle, and I thought it would be very useful to see examples of BAD as well as good code, with explanations.
 
Campbell Ritchie
Marshal
Posts: 52519
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Deitel book Java How to Program used to have an example of the producer‑consumer situation going wrong. There is a Bank example with a race condition in Core JavaII by Horstmann and Cornell, followed shortly by an example of a Swing app not using the event despatch thread exclusively. There are examples of deadlock (e.g. Dining Philosophers) and livelock in the Java™ Tutorials. There are probably other examples in the same Java™ Tutorials trail.
 
Benjamin Bh Weaver
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Sorry for the late response--I had trouble posting my question. Happy to see it went through]


Thanks, Campbell,

This is useful. I will check those examples out. In the meantime, I found a fair number of examples in Brian Goetz's Java Concurrence in Practice. Using your sources, JCIP, this and other forums I ought to be able to compile a fair collection of examples from which to learn.

Thanks again,

Ben Weaver
 
Campbell Ritchie
Marshal
Posts: 52519
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done
 
Przemyslaw Wojnowski
Greenhorn
Posts: 7
Clojure Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. Sorry for late repy.

Recently I've added posts describing common problems with synchronization and how to solve them. For example see Java synchronized method, where threads modify counter with and without synchronization. In further posts I'll describe problems with visibility (volatile) and atomicity.

Cheers.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!