Click here to Skip to main content
15,883,758 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
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
Updated 20-Jul-11 19:05pm
v7

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.
 
Share this answer
 
v2
Comments
Firo Atrum Ventus 21-Jul-11 21:33pm    
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:07am    
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900