One thread waiting on multiple other threads

Hi

I am sure this question has been asked before, but google is not being my friend

I have 3 functions incThreads_building, dec and wait.

when it comes time to do the notify it gives me a

Exception in thread "Thread-4" java.lang.IllegalMonitorStateException

I am a bit lost on this

public void incThreads_building() {

synchronized (threads_building) {

threads_building++;

}

}

/**

* Decrement thread count

*/

public void decThreads_building() {

synchronized (threads_building) {

threads_building--;

if (threads_building == 0) {

threads_building.notify();

}

}

}

public void waitOnthreads_building() {

// Wait for the whole window to build

// Including the sub frames

synchronized (threads_building) {

if (threads_building != 0) {

try {

threads_building.wait();

} catch (InterruptedException e1) {

e1.printStackTrace();

System.exit(0);

}

}

}

}

[1060 byte] By [KeyzerSuzea] at [2007-11-27 5:12:57]
# 1

But when I change the synchronized to around the methods instead of code blocks it works ?

public class ThreadTest {

public Integer counter;

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

ThreadTest t = new ThreadTest();

t.run();

}

public void run() {

counter = new Integer(0);

Thread workerThread;

for (int y = 0; y < 10; y++) {

workerThread = new Thread() {

public void run() {

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

incThreads();

try {

Thread.sleep(2000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

decThreads();

}

};

workerThread.start();

}

waitOnthreads();

}

public synchronized void incThreads() {

System.out.println("inc in");

counter++;

System.out.println("inc out");

}

/**

* Decrement thread count

*/

public synchronized void decThreads() {

System.out.println("dec in");

counter--;

if (counter == 0) {

notify();

}

System.out.println("dec out");

}

public synchronized void waitOnthreads() {

// Wait for the whole window to build

// Including the sub frames

System.out.println("wait int");

if (counter != 0) {

try {

wait();

} catch (InterruptedException e1) {

e1.printStackTrace();

System.exit(0);

}

}

System.out.println("wait out");

}

}

KeyzerSuzea at 2007-7-12 10:34:19 > top of Java-index,Core,Core APIs...
# 2

> I am a bit lost on this

yes, it seems so:

this forum is about SERIALIZATION and not synchronization :--)

better go to the [url=http://forum.java.sun.com/forum.jspa?forumID=534]Core APIs: Concurency[/url] forum...

there you can find something that can help you, almost same problem:

http://forum.java.sun.com/thread.jspa?threadID=5175590

Summary: not sure what threads_building is, but seems like it is not a good "Object" for locking, is it an object at all? or is autoboxing creating a new object (Integer) every time it is used?

Better use an dedicated Object for lockingfinal Object lock = new Object(); // used (only) for locking

[]

S_i_m_ua at 2007-7-12 10:34:19 > top of Java-index,Core,Core APIs...
# 3

this part is the culprit

public void incThreads_building() {

synchronized (threads_building) {

threads_building++;

}

when you change the value of threads_buildings using increment operator, it starts referring to the new object so your lock object changes and hence the exception. in case when you synchronize the function, the lock object is "this" which doesnot change and you don't face the issue...

gaurav_abbia at 2007-7-12 10:34:19 > top of Java-index,Core,Core APIs...