开发者

Need Help And Ideas In Solving A Collection Problem

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜