• Post Reply Bookmark Topic Watch Topic
  • New Topic

wait() and notify() problem  RSS feed

 
Daniil Sosonkin
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm having this problem when I'm trying to implement pool of threads. There is a pool of 5 threads which are pre-created at program startup like this:

... public synchronized void run() { wait(); ... do some work } ...

So each thread is waiting for the command which is issued like so:

... public synchronized void setSocket(Socket s) { this.s = s; notify(); } ..

Theoretically this should work fine. Once I set a socket to the next available thread it resumes its execution and once its done it is returned to the pool and waits for another socket. But the issue comes up when the pool is empty and a thread is needed. At this point in time, notification... gets lost? Basically, a new thread is created and a new socket is set right after its being created. But thread just waits and waits. I'm thinking that notify() is being called by creating thread before new thread enters wait() state.

Can someone help?

Thanx
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a bad idea to use wait() and notify() on java.lang.Thread objects, because these calls are used internally by the implementation of Thread itself. Instead, have each instance of your Thread subclass allocate a member variable -- just a plain java.lang.Object would do -- and wait/notify on that object instead, using synchronized blocks instead of synchronized methods. You will likely see your problem go away.
 
Daniil Sosonkin
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm, I'm getting this exception: java.lang.IllegalMonitorStateException: current thread not owner

Maybe I'm not doing something right. I have

private Object lock = new Object();

and in each method code looks like this:

synchronized (lock) { wait(); }
synchronized (lock) { notify(); }

And that throws an exception. Every tutorial that uses wait() & notify() synchronized based on method or on this (which is the same thing).
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Daniil Sosonkin:

synchronized (lock) { wait(); }
synchronized (lock) { notify(); }


You want to call wait and notify on the object you're synchronizing on -- i.e.,

synchronized (lock) { lock.wait(); }
synchronized (lock) { lock.notify(); }
 
Daniil Sosonkin
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, I think I understand the reason for that. There is another peice of code where one thread puts information into ArrayList and notifies another to take it which was completely wrong :roll: Just to be sure, code below would cause producer thread to wait for consumer to finish:

public synchronized void putMsg(String msg)
{ arrayList.add(msg); notify(); }

public synchronized void run()
{ ... wait(); while (arrayList.size() > 0) send(arrayList.remove(0)); }

As I understand, producer won't be able to get a lock for putMsg() until after run() goes into wait() once its done processing everthing from the arrayList.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!