简体   繁体   中英

Java program only works with Breakpoints in Netbeans

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM