The debugger has a hard time switching contexts and apparently
loses track if the next breakpoint happens in another thread as shown
in the two attached files.
Place a breakpoint at respectively readValues and writeValues of RWTest
and debug RWTest. The first times the debugger will stop at readValues
but then the next breakpoint seems never to be reached. The CPU is at
almost 0% and the JVM has clearly stopped, but Idea doesn't know this...
The same thing works like clockwork with Bugseeker. JDK used
java -version
java version "1.3.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)
socket or memory have same effect, but chosing classic seems to solve
the issue. The help file says otherwise...
Idea build 616
Anton
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp.
public class RWLock {
private int readers;
private int writersWaiting;
private boolean writingInProgress;
private int maxWritersWaiting = 1;
RWLock(int maxWW) {
maxWritersWaiting = maxWW;
}
synchronized public void getReadLock() {
while (writingInProgress | (writersWaiting >= maxWritersWaiting)) {
try {
wait();
} catch (InterruptedException ie) {
}
}
readers++;
}
synchronized public void releaseReadLock() {
readers--;
if ((readers == 0) & (writersWaiting > 0)) {
notify();
}
}
synchronized public void getWriteLock() {
writersWaiting++;
while ((readers > 0) | writingInProgress) {
try {
wait();
} catch (InterruptedException ie) {
}
}
writersWaiting--;
writingInProgress = true;
}
synchronized public void releaseWriteLock() {
writingInProgress = false;
notifyAll();
}
public int getReaders() {
return readers;
}
public int getWaitingWriters() {
return writersWaiting;
}
}
/*
* $Log:$
*/
public class RWTest {
int arr[];
String name;
RWLock lock;
public static void main(String args[]) {
RWTest rwtest = new RWTest();
}
RWTest() {
lock = new RWLock(3);
arr = new int[3];
name = "name";
for (int i = 0; i < 20; i++)
(new ReaderThread()).start();
for (int j = 0; j < 4; j++)
(new WriterThread()).start();
}
public void readValues() {
lock.getReadLock();
System.out.println("Read Lock obtained by " +
Thread.currentThread().getName());
System.out.println(" >>>> Readers active = " +
lock.getReaders());
System.out.println(" >>>> Writers waiting = " +
lock.getWaitingWriters());
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i] + " ");
System.out.println(name);
//PLACE BREAKPOINT IN NEXT LINE
System.out.println("Read Lock to be released by " +
Thread.currentThread().getName());
lock.releaseReadLock();
}
public void writeValues() {
lock.getWriteLock();
System.out.println("Write Lock obtained by " +
Thread.currentThread().getName());
int value = (int) (Math.random() * 100);
for (int i = 0; i < arr.length; i++)
arr[i] = value;
name = "" + value;
//PLACE BREAKPOINT IN NEXT LINE
System.out.println("Write Lock to be released by " +
Thread.currentThread().getName());
lock.releaseWriteLock();
}
class ReaderThread extends Thread {
public void run() {
while (true) {
try {
readValues();
Thread.sleep((int) (Math.random() * 50));
} catch (Exception e) {
}
}
}
}
class WriterThread extends Thread {
public void run() {
while (true) {
try {
writeValues();
Thread.sleep((int) (Math.random() * 100));
} catch (Exception e) {
}
}
}
}
}