Browse DevX
Sign up for e-mail newsletters from DevX


Use Callbacks to Isolate Concurrency Bugs : Page 4

By restricting all object data access to a callback mechanism, a Java server can contain all concurrency issues in a single place, making it much easier for you to see if concurrency constraints have been violated.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

The Pound Class (Testing the GateKeeper)

The GateKeeper is really the heart of this synchronization method, and the use() method is the heart of the GateKeeper. The use() method is fairly short and simple; it looks like it does what it's supposed to. But you still should test it thoroughly.

The Pound class does this for you. As its name suggests, Pound "pounds" on the GateKeeper, using it as fast as possible with lots of threads. Inside the GateKeeper, of course, is a Sum object.

You run Pound like this:

% java Pound 20

This creates 20 Pound objects, running in 20 threads. Each Pound object takes its turning modifying and verifying the sum object.

Since a Pound wants access to the Sum object, it must go through the gatekeeper, which means it is an Accessor and Mutator. Each time through its main loop, a Pound object flips a coin and does either an access (read) or a mutation (write). Here is its mutate() method:

public void mutate( Object o ) { Sum sum = (Sum)o;

First, you change either a or b:

// Change a or b. int delta = rand.nextInt( 2000 ) - 1000; if (rand.nextInt( 2 )==0) { sum.a += delta; } else { sum.b += delta; }

At this point, the data is in an inconsistent state: c does not equal a + b. This is precisely the time when you do not want anyone looking at the data.

Just to prove a point, do a yield() and a sleep(), allowing other threads to run. You want to make sure that your code works because it is correct, not because other threads didn't get a chance:

// The better to stress the thread-safety of the system. Thread.yield(); try { Thread.sleep( 20 ); } catch( InterruptedException ie ) {}

After your daring pause, correct your inconsistency and continue:

// Make sum correct again. sum.c = sum.a + sum.b; // Report. checkAndReport( sum, "mutate" ); pause(); }

Once the call to mutate() ends, other threads will get a chance to run. Your access() method is much simpler.

Sum sum = (Sum)o; // Just check the sum. checkAndReport( sum, "access" ); pause();

The only thing to do is output the values and verify that c does in fact equal a + b. If all is working correctly, it does.

You can easily test the code by running Pound with a good number of threads (say, 10 or 20) and letting it run for a while. It should report no errors.

Tracking How Many Threads Are Using An Object

I developed this technique because I was struggling with Java's garbage collector. I faced a situation where I was running out of memory, but the memory was allocated in native code, so the garbage collector was not running. I had huge objects that I wanted collected, but Java didn't know they were huge, so it didn't bother.

What I really needed was a way to find out when a particular object was no longer being used, but this is something that Java hides from you. The whole point of the garbage collector is to take care of such objects automatically.

Inspired by the idea of linear variables and monadic state, I decided that what I needed was a single-threaded or linear object—one that would be accessed by only one thread at a time (although I stretched this idea a bit to allow multiple readers). By using this structure, I was able to more precisely control and track the number of threads using an object, which was exactly what I needed to know when an object was no longer in use.

Greg Travis is a Java programmer and technology writer, living in New York City. After spending three years in the world of high-end PC games, he joined EarthWeb, where he developed new technologies with the then-new Java programming language. Since 1997, he has been a consultant in a variety of Web technologies.
Thanks for your registration, follow us on our social networks to keep up-to-date