views:

168

answers:

2

I'm working on a multithreaded program in Java that uses a shared array to pass data between threads. It's being developed in Netbeans 6.7.1.

One of the threads only seems to work when a breakpoint is placed in it, it doesnt matter where it is.

Running in debug mode with no breakpoints acts the same as running in release - the expected output never arrives.

I can't tell where the problem occurs, as the moment a breakpoint is added and I press continue, it works as expected.

How can I narrow down where/why this problem occurs?

Example code:

result = utils.isBufferFull(AudioDuplex.voiceArray);
if(result == true) {
    System.out.println("Taking copy");

    voiceArray = AudioDuplex.voiceArray;//.clone();
    utils.clearBuffer(AudioDuplex.voiceArray);

 }

If a breakpoint is placed on line 2, it is never hit. A breakpoint on line 3 will be hit, and the expected output will arrive.

+1  A: 

Write the values of the involved variables to a log file, the console or add them to an array and print them as soon as you get the error.

Your problem is probably a runtime issue (a second thread updates an involved variable). Since breakpoints only stop the active thread, the second thread gets its work done so the code works.

Aaron Digulla
I've tried pausing it using sleep(), but it doesn't work. The most confusing issue is the strange breakpoint behavior, I've added a code example to the question.
dig412
Which version of Java?
Aaron Digulla
+1  A: 

It's impossible to tell exactly what's wrong without a lengthier code sample, but in my experience, this kind of behavior is typical of unrecognized producer-consumer problems (see http://en.wikipedia.org/wiki/Producer-consumer_problem).

Basically, what's probably happening is that your producer thread does not have the data available when the consumer thread is requesting it. The basic solution is to keep a semaphore (there is a Sempahore class in java afaik). The producer would post when it has the data, the consumer would wait until the producer posts.

What you're seeing with the break point is you stopping the consumer thread for a long enough period that the producer can offer something. When you don't break, the consumer runs normally, and exits before the producer has anything.

laura
After a couple of re-runs the breakpoint issue vanished, and adding a 10ms sleep before the problematic thread checks the array fixed the issue. Thanks for your advice!
dig412
That doesn't "fix" the issue, it just makes it disappear. You must properly synchronize the two threads or it will break again under load.
Aaron Digulla