Win a copy of Kotlin in Action this week in the Kotlin 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
Greenhorn
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();
lstRunnableHolder.add(iUserCount,oWorkerThread);
Thread thWorker = new Thread(oWorkerThread);
lstThreadHolder.add(iUserCount,thWorker);

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));
lstThreadHolder.add(oWorkerThread);

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

try
{
System.out.println("IN WAIT STATE");
while(CSystemConstants.ready == 0)
{
wait();
}
}
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;
notifyAll();
}//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 CWorkerThread.run(CWorkerThread.java:67)
at java.lang.Thread.run(Unknown Source)

-----

I would really appreciate if someone can please help me with this,
cant figure it out!
 
Henry Wong
author
Sheriff
Posts: 23283
125
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.

Henry
[ December 16, 2004: Message edited by: Henry Wong ]
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!