• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why notify/notifyAll() method doesnt invoke a waiting thread quickly?????

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am currently working on a socket based IM server, I get messages from multiple clients simultaneously and put them into queues, for each queue in my server I have a separate listener running in a thread, as soon as a packet comes in the queue (from any client) it�s related thread is notified to get that packet from thread and process it.

Here I am using wait/notify blocking mechanism to control loop in my listener thread. My logic works like this

1)When Listener thread starts it calls the synchronized getNextPacket method on that queue if the queue has any packet (i.e. it�s length is greater then zero) then it will remove that packet from the underlying vector (NOTE: I am using Vector to contain packets) and return that packet to listener thread, else if queue doesn�t have any packet (i.e. its size is zero) then I will call wait() method which causes the listener thread to wait.

2)Now when any client adds some packet in that queue using synchronized addPacket( ) method I first add that packet in the underlying vector and then call notify()/notifyAll() to awake that listener thread, which again calls the getNextPacket() method and this cycle continuous like this.

So multiple threads (clients) are adding data in the queue using synchronized method, only one listener thread is getting data from that queue using a synchronized method again  .
This approach works fine but sometimes I have noticed that the listener thread doesn�t resume just after notiy() / notifyAll() has been called , sometimes it resumes after a long time sometimes it even don�t resume (after waiting a long time I assumed this).

Solutions I tried
1)I did set the listener Thread�s priority to Maximum but facing same problem again.

For better understanding I am also sending you the code for my queue class and it�s listener thread.

CODE OF QUEUE CLASS



CODE OF LISTENER CLASS


Can anybody please tell me where I am doing something wrong and whats the best way to get rid of this situation .
Thanks in advance
[ April 22, 2007: Message edited by: sajjad ahmad ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looking at your code, I don't see a cause for the behavior your describe. However the getNextCommandPacket() does seem rather strange. It looks like any time it can't find a command, it will wait, and then return null. It is guaranteed to return null after a wait. Why do that? Isn't the purpose of the wait to wait until you can return something besides null? I recommend you put a loop inside the getNextCommandPacket() method, so that the method rechecks if size() > 0 after each wait. Once you have size() > 0, then you can return. And then there's no need to check for null later.

Refactoring the code a bit may make the problem go away. If not, I would recommend adding more logging. In particular add log statements immediately before and after the wait() call, and before and after the notifyAll(). These log statements can also include the name of the thread doing the logging. In log4j you can get this wasily by including %t in the PatternLayout configuration. Or you can print it with Thread.currentThread().getName() if you need to. This will be useful to tell you which thread is doing what.

The objective here is to discover if the listener thread is really in the wait() method when notifyAll() is being sent, or if it's doing something else. If it's doing something else, then you can add more logging statements elsewhere in the program to discover just what he listener is doing when the notifyAll() is sent. That will help increase understanding of what's really happening. Hope that helps...
 
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try a sleep(1) after returning from addCommandPakcet in the other thread (not in the sync'ed block).

Also your wait should really be in a loop of some form i.e. spurious thread wake up (rare but possible for wait to return without notify)
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suspect by the time you call notify, many of your threads have already returned and are not waiting. So it may look like they are ignoring the call, but its because they were no longer waiting when the call was made.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic