需要帮助和想法来解决集合问题

发布于 2024-09-12 01:19:05 字数 734 浏览 7 评论 0原文

我今天已经发布了一个问题。这个问题是关于同一个项目但不相关的。我正在为 Lego NXT Mindstorm 机器人开发一个应用程序。我有两个机器人和一个在 PC 上运行的 GUI。

在 leJOS NXJ 中您只能使用一个输入阅读器。这意味着你不能将PC直接连接到两个机器人并让两个机器人直接相互连接。这就是我所做的。我已将 PC 直接连接到两个机器人,当两个机器人想要直接通信时,我通过 GUI 发送它们的消息。

GUI 和机器人之间以及机器人本身之间有大量的通信。因此,每当我将数据写入输出流时,某些数据似乎会被其他数据覆盖,并且系统无法按预期正常工作。

有人建议我编写一个保存集合(队列)对象的类,以便每当机器人想要发送某些东西时,它都会将其添加到集合(队列)中,并且从保存集合对象的类中,将会有一个方法,以便它不断检查集合,只要它不为空,就将集合中的数据发送到输出流。

这意味着每当集合中的数据被发送到输出流时,就有可能添加新数据。

有些人建议我使用 ArrayBlockQueue 等。但这些类在机器人使用的 class.jar 文件中不可用。

我在这个 jar 文件中知道的集合类是 Vectors 和 Queue。

我问是否有人可以帮助我,给我如何实施此类课程的想法。类中的方法将不时检查集合中是否有数据,并将通过输出流发送它们。在发送时,可能会添加新元素。

由于数据是从一个地方发送的,因此任何数据都不会覆盖另一个地方。在我看来这是一个好主意。

欢迎您提出所有建议。

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

<逆流佳人身旁 2024-09-19 01:19:05

Vector 很好,因为(至少在 JavaSE 中 - 我不知道 Mindstorms 使用什么)它是同步的,所以所有调用都是原子的 - 如果另一个线程在你从 Vector 中删除时尝试向它添加某些内容,它会阻塞,直到您已完成,避免了数据丢失的问题。

或者,您可能想查看同步包装器< /a> 在 Collections 类中。

或者,您可以通过子类化标准队列来实现自己的阻塞队列。虽然更复杂,但阻塞队列是一个更好的解决方案,因为它避免了繁忙的等待,即您反复检查队列,每次都被告知它是空的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文