• 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

Wait and notify for sending bunch of messages

 
Ranch Hand
Posts: 149
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Would be super grateful if someone can explain me how wait/notify/notifyAll works and if is there better solution for the problem I am facing. Basically, we have to send a bunch of SMS messages. For sending messages an object called SMPPSession is used but in this example I'll just use superficial code. SMPPSession is supposed to send messages to SMSC server and to reestablish session in situations when connection breaks.  I would like to use multiple threads for sending multiple messages, and to have a separate single thread, some sort of "guardian"/ "watcher"/"notifier".  The role of that separate thread is to stop all other threads from executing their code, while it works on reestablishing session. Naturally, SMPPSession is shared among all these threads. Once that guardian finishes reconnecting, all other thread needs to continue with using the session and proceed with sending.

Now, I have some code and getting exception. Any help?

In reality we do send real SMS messages using jsmpp library and inside it there is SMPPSession object.



In order to make it visual:

Untitled.png
[Thumbnail for Untitled.png]
 
Bartender
Posts: 2416
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may need to check if the array is empty, then the thread needs to wait until the array has at least one element.
 
Ranch Hand
Posts: 180
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should check the size of the messages only after acquiring the lock.
Basically, your while loop should be inside the "critical section".
Then your waiting condition in the SessionProducer becomes:

 
Himai Minh
Bartender
Posts: 2416
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand why we have to synchronize the messages collection first before we perform the size check.
Synchronize on an empty collection costs time. In the other words, synchronization is expensive and better not to
use it on the empty list.
 
Himai Minh
Bartender
Posts: 2416
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I changed the run method of Sender like this:


Output:


Sending message: msg1
Sender3 sent msg and received msgId: 548773298
Sending message: msg2
Sender3 sent msg and received msgId: 1882823304
Sending message: msg3
Sender3 sent msg and received msgId: 510749702
Sending message: msg4
Sender3 sent msg and received msgId: 1285812849
Sending message: msg5
Sender3 sent msg and received msgId: 1367977611
Sending message: msg6
Sender3 sent msg and received msgId: 1066370205

Process finished with exit code 0


Only one sender thread is sending  messages. It is because it first synchronizes the Client.messages. And while the message is not empty, it keeps  removing one message, send another message. It notifies other threads. However, other threads cannot acquire the lock for the Client.messages. Basically, other threads are not in waiting state. So, this active thread notifies nobody.
 
Himai Minh
Bartender
Posts: 2416
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more note. If you do this in the run method of Sender:


The output will be like the following , but the program will not exit because there is a while loop (!client.message.isEmpty()). The messages is not empty and the smppSession reBind() makes the bind = true, so the producer waits forever.

Rebinding...
Session established!
SessionProducer1 managed to reestablish SMPP session.
Sending message: msg1
Sender3 sent msg and received msgId: 105382553
Sending message: msg2
Sender4 sent msg and received msgId: 1361595867
Sending message: msg3
Sender2 sent msg and received msgId: 626877645
Sending message: msg4
Sender1 sent msg and received msgId: 147902866

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic