This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

I have multiple threads and am trying to have one thread control them all  RSS feed

Guneet Aulakh
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK.. This is the first time I try this forum. I am new to programming. There is this program where I need to create multiple threads for:

1. To create a set off threads, initiate them and then have them run after a condition is met. I was thinking of wait/notifyall here!

2. I want to create threads, but they should start running at different times. In this situation I was thinking of using the sleep command while creating the thread and then placing a random number in it for the thread to sleep for.

Here is the code:

This is where I initialize the thread in the Main:

oWorkerThread = new CWorkerThread();
Thread thWorker = new Thread(oWorkerThread);

This is how it is really created in the Main:

((CWorkerThread)lstRunnableHolder.get(iUserCount)).startThread(new CUserDetails(iUserCount,ptLocation,0),FirstMicroCell,SecondMicroCell, ThirdMicroCell,AreaDefn,p_lTimeInSystem,oFileHandler,(Thread)lstThreadHolder.get(iUserCount));

Here is the CWorkerThread class and in the run where I am using the wait:

System.out.println("IN WAIT STATE");
while(CSystemConstants.ready == 0)
catch (InterruptedException eIE)

This is CMakeThreadRun class and this is the thread that issues the notifyall:

public synchronized void MakeRun()
// coming from the constants and is used in main to set this thread to call notifyall
CSystemConstants.ready = 1;
}//close MakeRun


The threads are created and all, but there is an error that I get

java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Unknown Source)
at Source)


I would really appreciate if someone can please help me with this,
cant figure it out!
Henry Wong
Posts: 22846
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Short answer:

You need to be holding the synchronization lock of the object you are using to wait/notify:

Long answer:

There is a race condition between checking the flag, and waiting, and setting the flag, and notifying. Basically, notification can be lost.

To solve this, synchronization is needed. However, there is still a race condition between releasing the lock and calling wait(). So... to solve this, the wait() method releases the lock for you, and does so in a thread safe manner... so... You need to be holding the synchronization lock of the object you are using to wait/notify.

[ December 16, 2004: Message edited by: Henry Wong ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!