Click here to Skip to main content
11,478,066 members (75,174 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Algorithms
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 16:30pm
Edited 20-Jul-11 20:05pm
v7
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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  
v2
Comments
Firo Atrum Ventus at 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 at 22-Jul-11 3:07am
   
Answer expanded to deal with more than two threads.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
0 Sergey Alexandrovich Kryukov 184
1 OriginalGriff 160
2 Frankie-C 160
3 Sascha Lefèvre 145
4 F-ES Sitecore 120
0 Sergey Alexandrovich Kryukov 7,675
1 OriginalGriff 7,256
2 Sascha Lefèvre 3,034
3 Maciej Los 2,491
4 Richard Deeming 2,305


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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100