views:

46

answers:

1

Hi Guys,

I have already posted a question today. This question is about the same project but unrelated. I am developing an application for the Lego NXT Mindstorm robot. I have two robots and a GUI running on a PC.

In leJOS NXJ you can only use one input reader. This means that you can't connect the PC to two robots directly and let the two robots connect to each other directly. So this is what i have done. I have connected the PC to the two robots directly and and when the two robots want to communicate directly, i send their messages through the GUI.

There is a whole lot of communication between the GUI and the robots as well as between the robots themselves. For this reason anytime i write data to the output stream it seems that some of the data are overwritten by others and the system is not working correctly as suppose to.

I have been advice to write a class that will hold a collection(Queue) object so that anytime the robot want to send something, it add it to the collection(Queue) and from that class which hold the collection object, there will be a method so that it checks the collection constantly and whenever it is not empty, it sends the data in the collection to the output stream.

It means that whenever the data in the collection are been sent to the output stream, it is possible a new data is been added.

Some people suggested to me of using ArrayBlockQueue and etc.. but those classes are not available in the class.jar file which the robot uses.

The collections classes that i know in this jar file are Vectors and Queue.

I am asking if someone can help me by giving me ideas of how to implement such class. A method in the class will check from time to time if there are data inside the collection and it will send them through the output stream. While it is sending , it is possible that new elements are being added.

Since the data are being sent from one place, no data will overwrite the other. It sounds to me as a good idea.

All your suggestions are welcome.

Thanks.

A: 

Vector is good because (at least in JavaSE - I don't know what Mindstorms uses) it's synchronized, so all calls are atomic - if another thread tries to add something to the Vector when you're removing from it, it will block until you have finished, avoiding the issue where data can get lost.

Alternatively, you may want to have a look at the synchronization wrappers in the Collections class.

Alternatively, you could do your own implementation of a blocking queue by subclassing a standard Queue. Although more complicated, a blocking queue is a better solution, as it avoids a busy wait, where you repeatedly check the queue and are each time told it is empty.

Scott
I would suggest composition instead of subclassing, that is safer in the long term. You want to ensure that _all_ public methods of your class are synchronized, but if the superclass interface is extended in the future, that guarantee is broken.
Péter Török
Good point. I'd assumed that subclassing was what Java's blocking collections did, as they have a slightly different interface, but having had a closer look I see they must be using composition (or similar).
Scott
Thanks for your replies guys
kap
You're welcome. Did this solve your problem? If so, please consider accepting it, otherwise feel free to ask for more detail or alternative solutions.
Scott