January's Code Share: Concurrency

The Concurrency Revolution

At the end of 2004, in his article "The Free Lunch is Over", Herb Strutter wrote:

"Concurrency is the next major revolution in how we write software... concurrency is of the same order as OO both in the (expected) scale of the revolution and in the complexity and learning curve of the technology."

The world of hardware has progressed rapidly, and we enter 2012 with dual core phones in our pockets.  Is our software keeping up with the pace?

Before our software can be concurrent we programmers must learn to think concurrently. Strutter continues:

"Probably the greatest cost of concurrency is that concurrency really is hard...  Everybody who learns concurrency and thinks they understand it, ends up finding mysterious races they thought weren’t possible, and discovers that they didn’t actually understand it yet after all"

What is the best way to tackle this difficult problem? What affect does the need for concurrecny have on our code? Should we turn to functional programming and languages such as Clojure and Scala. In his MSDN Blog Brandon Werner observes:

In the living world of mutable objects and variables, breaking out two transactions to work concurrently that operate on the same living data is a bad idea. Add structural programming's solution to this problem, optimistic and pessimistic locking, and you have dead-locks in short order. Functional programming has been a natural place to explore parallel processing and new ways of doing atomic transactions because of the reasons above.

An alternative approach is Mechanical Sympathy, gaining an understanding of what is happening within the machine allows us to understand the pitfalls and identify the opportunities. In his article about the LMAX Disruptor architecture Martin fowler notes that this is essential for successful performance testing:

Mechanical sympathy is important to developing the right tests. Testing a low level concurrency component is meaningless unless you take into account the caching behaviour of the CPU.

Let's get together and look at the approaches we are taking for concurrency. What does it look like in the code we are writing? Do we have to sacrifice readability? Is it necessary to learn a new language? Is an understanding of the Java Memory Model essential?

Coding Challange: Test Driving Concurrency

Testing for concurrency problems is difficult because the fault may only occur under specific circumstances.

The challange for this month is to demonstrate some test driven development for concurrency problems.

Can you write a test case that succinctly demonstrates the following concurrency problems?

  • Race Conditions
  • Deadlock
  • Starvation
  • Nondeterminism

You may find the sample third chapter from "High-Performance Java Platform Computing" useful: Race Conditions and Mutual Exclusion.

Here is my attempt. Clearly we can be do better. Is jUnit the right tool? Do other languages make it easier?


import org.junit.Test;

public class TestDrivingConcurrency {

 @Test()
 public void singleThreading() {
  new BuildTestString(0).start();
  while(results[0] == null) {}
  org.junit.Assert.assertEquals("[masked]", results[0]);
 }

 
 @Test()
 public void multiThreading() {
  
  new BuildTestString(1).start();
  new BuildTestString(2).start();
  new BuildTestString(3).start();
  new BuildTestString(4).start();
  
  while(
    results[1] == null 
    || results[2] == null
    || results[3] == null
    || results[4] == null) {}
  
  org.junit.Assert.assertEquals("0123456789012345678901234567890123456789", results[1]+results[2]+results[3]+results[4]);
 }

 
 static int i;
 static String result;
 static String[] results = new String[5];
 
 class BuildTestString extends Thread {
  
  int resultIndex;
  public BuildTestString(int resultIndex) {
   this.resultIndex = resultIndex;
  }
  
  public void run() {
   for(i = 0, result = "";i<=9;i++) {
    try {
     sleep(100);
    } catch (InterruptedException e) {
     org.junit.Assert.fail(e.getMessage());
    }
    result+=i;
   }
   results[resultIndex] = result;
  }
 }
 
}

It gives the following failure:


org.junit.ComparisonFailure: expected:<01234567890123456789[01234567890123456789]> but was:<01234567890123456789[10012345678910111201234567891011]>
 at org.junit.Assert.assertEquals(Assert.java:123)
 at org.junit.Assert.assertEquals(Assert.java:145)
 at TestDrivingConcurrency.multiThreading(TestDrivingConcurrency.java:27)

What's Going To Happen?

On Monday 9th January, a couple of days before the share, we'll send out an email to everybody who has signed up. If you have any code to contribute please send it in a reply to that email.

After the event we will be heading to the Half Moon, [masked] Mile End Road, Mile End, Greater London, E1 4AA - http://www.jdwetherspoon.co.uk/home/pubs/the-half-moon - for drinks/networking.

This is a joint event with the Graduate Developer Group - http://www.meetup.com/grad-dc/events/44932902/ - providing an opportunity for both experienced developers and newcomers to the profession to share points of view.

Please Note -

At previous events LJC members have found this venue/particular room hard to find.

This event is being held in the Francis Bancroft Building - Room Number - FB 1.13. 

Please see this map for further information (the building is numbered 26 on this map) http://www.qmul.ac.uk/docs/about/26065.pdf 

The actual signs at the building will say 'Bancroft' building only rather than Francis Bancroft.

Directions -

From Mile End Station you will need to turn left out of the station and then continue for 5 mins approx. up the Mile End Road and then cross over the road to the QM campus and go into the East Gate.  This is the first entrance to QM you come to when coming from the direction of Mile End Tube.  Continue straight on up Westfield Way for a couple of minutes and then bear left at the Curve cafe.  The Bancroft Building is on your right (number 26 on the map).

Join or login to comment.

  • Abdenour B.

    It was great meetup, thanks guys for the work.

    January 16, 2012

  • Peter L.

    Updates slides for Java Thread Affinity including the ability to define groups of threads and reserve a whole core to a thread. https://github.com/peter-lawrey/Java-Thread-Affinity/blob/master/docs/Java_Thread_Affinity_library.pdf

    January 13, 2012

  • James T

    very good hard to understand why you had limited spaces - there were plenty of free seats

    January 12, 2012

  • Ged B.

    There's also http://code.google.com/p/multithreadedtc/

    From the poster: "The MultithreadedTC framework was created to
    make it easier to test small concurrent abstractions. It enables test designers to validate each interleaving of two or more threads separately, even in the presence of blocking and timing
    issues. It also detects deadlock situations"

    It look's very academic at the moment, but could have potential.

    It was developed by f Bill Pugh and Nat Ayewah.

    January 12, 2012

  • Michael

    I have just remembered about this: http://groboutils.sourceforge.net/

    January 12, 2012

  • Andy D.

    Wasn't sure what to expect but really enjoyed this. Some very good speakers and really learned alot. Next time when I haven't got a stinking cold i'll come to the pub too!

    January 12, 2012

  • Graham A.

    Also discussed in our group was a tool from IBM for multi-threaded unit testing, which I couldn't remember the name of. Just found out it is named ConTest, there's an article on it here: http://www.ibm.com/developerworks/java/library/j-contest/index.html (though that was 2006, don't know how active the project is now).

    January 12, 2012

  • Ged B.

    I learned a lot.

    January 12, 2012

  • Peter L.

    @Graham, Great tool. :)

    January 12, 2012

  • Graham A.

    The homepage of Mutability Detector I presented is www.mutabilitydetector.org

    January 11, 2012

  • Ioannis M.

    Very nice mix of presentations and problem solving.

    January 11, 2012

  • A former member
    A former member

    Please give the spot to me :-)
    You can contact me on[masked] or [masked]

    January 11, 2012

  • Dominic

    Just been noticed of a spot but can make it tonight. Kindly offer it to someone. Thanks.

    January 11, 2012

  • Jarek K.

    Sorry, I have to cancel my attendance last minute.

    January 11, 2012

  • Maris O.

    my code for review
    git://github.com/maruks/clojure-stm-example.git

    1 · January 11, 2012

  • A former member
    A former member

    Sorry, have to drop out from tomorrow's session. Catch up later

    January 10, 2012

  • Ged B.

    @Vinay,

    Sorry, we really are full to capacity. The only hope now is for drop outs.

    January 10, 2012

  • Ged B.

    @Peter: I'm just sending out the email now. I'm looking forward to hearing more.

    January 9, 2012

  • Peter H.

    @Ged: Has the email to invite people to contribute gone out yet? I have a couple of real-world failures of concurrency tests that might be interesting - anti-patterns, if you will, of things that looked like they were good tests but turned out not to be...

    January 9, 2012

  • Ged B.

    We've moved 10 spots over from the GDC event. This is enough to include everybody currently on the waiting list.

    January 4, 2012

  • Ged B.

    @Peter, there looks to be a lot of interest. I'm hoping we can identify different areas of concurrency that we can drill down on.

    December 20, 2011

  • Peter L.

    @Ged, Like you said, there are spaces free for Graduates. An mix of audience is a good thing. We can always have more sessions on concurrency later in the year. I was surprised how quickly the spaced filled. :-)

    December 20, 2011

  • Fayimora F.

    Sorry do you mean sign up for this event?? If yes then I already signed up!

    December 20, 2011

  • Ged B.

    Peter,

    I afraid we're going to have to give it some serious thought before adding any more spaces. Too many people can spoil the sharing environment.

    December 20, 2011

  • Ged B.

    Fayimora,

    There are still 18 spots left on the GDC side. As a member you can sign up there.

    December 20, 2011

  • Fayimora F.

    Hey guys, what's with the "waiting list" .. Am I missing something??

    December 20, 2011

  • Peter L.

    +10 spaces taken already. ;)

    December 20, 2011

  • Ioannis M.

    Sounds like it is going to be crowded I better wash :-P

    December 20, 2011

  • Ged B.

    We've added another 10 spaces.

    1 · December 20, 2011

  • Mukhan M.

    Is it possible to get a larger classroom in Queen Mary University :). on a waiting list...

    December 20, 2011

  • Ged B.

    The waiting list has been added.

    December 20, 2011

  • A former member
    A former member

    Yes, waiting list please

    December 19, 2011

  • Ioannis M.

    Waiting.List.Please :-/

    December 19, 2011

  • Ged B.

    Thanks Peter. Fixed.

    December 15, 2011

  • Peter L.

    s/duel/dual/ ;)

    December 15, 2011

37 went

Our Sponsors

  • Our Blog

    Read the latest news from the LJC

  • RecWorks Ltd

    Fixing Tech Recruitment using the Power of Community

  • jClarity

    Java/JVM Performance Analysis Tools & mentoring for Java related matters

  • LJC Aggrity

    Our LJC Aggrity site contains blog posts from our members

  • LJC Book Club

    Our Book club with book reviews from our members

  • Devoxx UK

    Java Community Conference, in collaboration with the LJC 12/13 Jun 14

  • SkillsMatter

    "Host, help organise, promote, film many of our meetings."

  • Packt Publishing

    A publishing company specializing on specific technologies and solutions

  • Java.Net

    We are an official Java User Group recognised by Oracle's JUG program

  • JRebel

    Free 3 month J-Rebel license.

  • O'Reilly

    40% discount on printed books and 50% on e-books.

People in this
Meetup are also in:

Create a Meetup Group and meet new people

Get started Learn more
Bill

I started the group because there wasn't any other type of group like this. I've met some great folks in the group who have become close friends and have also met some amazing business owners.

Bill, started New York City Gay Craft Beer Lovers

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy