• Post Reply Bookmark Topic Watch Topic
  • New Topic

Ugh...a little help please

 
Jason Steele
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy,


After searching, and maybe just not understanding, I have finally decided to appeal to the masses. Here is my problem:

1. I have one class that implements runnable - MainClass
2. Then I have two seperate thread classes InitThread, ListenerThread
(MainClass is a Client)

In MainClass I read in properties for network settings

After reading properties and setting initialization vars, I call connect() which looks as follows:


Now, InitThread does do what it is supposed to do...but then I get this:
java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.wait(Native Method)


I have found a lot on the exception, but I do not know how to fix it...What I want is for the listenerThread to wait until the initThread is done...any suggestions would be appreciated.

 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm the last person to criticize someone's design, but why create a bunch of threads if you aren't doing anything concurrently?
In order to solve your problem you have to have the current thread obtain the lock the object you are waiting on. One accomplishes this with the synchronized block:

Another problem may be lurking in the shadows. It sounds like the code in your InitThread.run() method is finishing before wait gets called in MainClass. If you are depending on a notify() call in InitThread.run() to clear that wait, it probably gets invoked before wait() gets called. That would mean MainClass would block on wait() forever.
Take a gander the Java Tutorial: Threads for a quick introduction to threading.
 
Jason Steele
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the prompt reply...and your critism is well taken. I will try that.
 
Yaroslav Chinskiy
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why dont you try join() method?

if you need thread T1 to wait for completion of thread T2 then:
in T1
try{
T2.join();
}catch(InterruptedException ie){
// do something
}

This will suspend T1 while T2 active

Hope that helps.
 
Jason Steele
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yaroslav,

Thanks, this sounds good. I will try it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!