Click here to Skip to main content
Sign Up to vote bad
good
See more: JavaAlgorithms
Hi all,
 
I'm currently debugging a ship monitoring system. In that application I found whole heaps of bugs, fortunately I can solve every single one of them, except this one. So please help me.
 
Here's the explanation:
The bug is located within a distribution server, and I suspect (No, I'm sure of it) that there's an extreme race condition between the receiver class (will be addressed as Receiver) and sender class (will be addressed as Sender).
 
Distribution server works like this:
1. Receiver receive data.
2. Receiver write data into a data storage class (will be addressed as DataStorage).
3. Sender check whether DataStorage is empty or not.
4. If DataStorage is not empty, Sender will proceed to send the data.
Or so they think.
 
Problems I found so far:
1. A guaranteed race condition 'cause Sender check using a flag.
Okay, I've applied a lock into Sender.
2. I can't safely release Sender's lock.
Why? it's simple, really. It's because receiver works as interface. It receive data when onDataReceived method is called by a jar file (it's called QuickServer).
 
So here I am, looking for some advice regarding this matter. The only solution I've found so far is:
1. Release Sender's lock when a new data is received.
2. Sender will raise a flag to suspend any Receiver's incoming data.
3. Sender wait for any unfinished operation by Receiver (Thread.sleep(500)?).
4. Sender grab all data.
5. Sender pull down the flag.
6. Receiver start it's suspended operation, Sender process and send the data.
7. Everyone live in harmony till Sender's lock is re-applied after sending data therefore go back to step1.
 
[edit]My solution failed,[/edit]Is there any other solution? I'm a fresh graduate from a vocational high school therefore I'm looking for some experienced expert's solution.
 

Thanks in advance,
 
Firo A.V.
Posted 20 Jul '11 - 15:30
Edited 20 Jul '11 - 19:05


2 solutions

You need to use a wait and notify[^] structure.
Simply put the when the sender gets to an empty queue, it goes into a wait state, and when the receiver adds something to the queue it notifies.
 
If all the Senders and Receivers are running within the same JVM, then you can ensure that someone gets the lock by putting all the logic in Synchronized blocks and using notifyAll() to wake up ALL the threads, rather than notify() which will wake up one and only one thread.
  Permalink  
Comments
Firo Atrum Ventus - 21 Jul '11 - 21:33
The problem is the Receiver works as interface which called by QuickServer. There's so many thread that invoke Receiver so when I notify sender, other Receiver may processing the queue (which already removed by sender)
Nagy Vilmos - 22 Jul '11 - 3:07
Answer expanded to deal with more than two threads.
You can get some inspiration from here: http://en.wikipedia.org/wiki/Producer-consumer_problem[^]
 
Here your Receivers are the Producers and your Sender is a Consumer.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Mohammed Hameed 268
1 OriginalGriff 261
2 Mayur_Panchal 153
3 Sergey Alexandrovich Kryukov 148
4 CPallini 96
0 Sergey Alexandrovich Kryukov 8,171
1 OriginalGriff 6,246
2 CPallini 3,532
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 27 Jul 2011
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid