Port of schuchert.wikispaces.com


TddAndConcurrency.Slides.WhenItsQuietBeAfraidVeryAfraid

TddAndConcurrency.Slides.WhenItsQuietBeAfraidVeryAfraid

Up

When It’s Quiet Be Afraid, Very Afraid

Quick Quiz. Is this code MT Safe?

public class ObjectWithValue {
    private int value;
    public void incrementValue() { ++value; }
    public int getValue() { return value; }
}


What’s Happening?

++value is actually three operations:

Since a thread can be preempted at any time…

Byte-code Description Operand Stack After
aload 0 Load local variable 0 onto the stack. In a non-static method, local variable 0 is always the “this” pointer, or the object that received the most recent message. this
dup Place a copy of the top operand on the operand stack back onto the operand stack. this, this
getfield value Retrieve the field named “value” from the object pointed to on the top of the operand stack, which is “this”. Put the resulting value back on to the top of the operand stack. this, 42
iconst_1 Put the constant value 1 onto the operand stack. this, 42, 1
iadd Perform integer addition on the top two items on the operand stack and place the result back on the operand stack. this, 43
putfield value Put the top item on the stack (43) in the “value” field of the object pointed to by the next to top item on the operand stack (this). «empty»
return Return back to the place where this method was called.  

How Many Possibilities?

Assume the following: Two threads

Answer the following:


Answers

How many possible orderings?

How can we calculate it?

title: TddAndConcurrency.slides.WhenItsQuietBeAfraidVeryAfraid.NumberOfCombinations — (\frac{(N * T)!}{N! ^ T})

</div>

How many possible outcomes?

How?


Sharing Without Guarding

@Test
public void singleThreaded() throws InterruptedException {
    Thread t = new Thread(incrementValue);
    t.start();
    t.join();
    assertEquals(10000, object.getValue());
}

@Test
public void multipleThreadsFail() throws InterruptedException {
    Thread[] threads = new Thread[5];

    for (int i = 0; i < threads.length; ++i)
        threads[i] = new Thread(incrementValue);

    for (Thread t : threads)  t.start();
    for (Thread t : threads)  t.join();
        
    assertTrue("Expected 10000, actual: " + object.getValue(),
        50000 == object.getValue());
}

Demo

Let’s demonstrate that this code is broken

ConcurrencyDemo_increasingChangesOfFindingDefect from Brett L. Schuchert on Vimeo.

Up


Comments

" Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.