java中多线程共享数据
当我在android上编写实时心电图监测程序时遇到问题。 心电数据通过udp实时传输到手机。在移动设备中,有两个线程:一个线程获取传输的心电图数据,一个线程绘制心电图数据。 循环缓冲区是上面两个线程的公共数据,两个线程在读写缓冲区时总是会发生冲突。结果是心电图丢失或缓慢。 在使用循环缓冲区之前,我使用了 5 个 linkedblockingqueu 但结果是相同的。
任何人都可以为我的程序中的多线程数据提供一些解决方案吗? 谢谢。
抱歉,我的英语不好。!
我使用linkedblockingqueue时有模型:
i have a problem when i write a program on android for monitoring ecg real time.
Ecg data is transfered to mobile in real time by udp. In mobile, there have 2 thread: a thread gets ecg data transfered, a thread draws the ecg data.
Cicurlar buffer is common data for two thread above, and two threads always confict when read and write to buffer. And result is that ecg is lost or slow.
Before user cicurlar buffer, i had used 5 linkedblockingqueu but result was same.
Can any one give me some solution for data for multithread in my program?
Thank you.
Sorry, my english is not good.!
there is model when i used linkedblockingqueue:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要使用共享锁来同步对数据的访问。如果您想真正了解 Java 中的线程和并发模型,我强烈推荐 Java 并发实践。
You need to synchronize access to your data using a shared lock. I highly recommend Java Concurrency in Practice if you want to truly understand threading and concurrency models in Java.
我认为,
同步
是您的问题的解决方案。来自 JavaDoc 的
BlockingQueue
I think,
Synchronization
is the solution for your problem.From the JavaDoc's
BlockingQueue
在我的假设中,您直接访问集合(基于任何 Fifo),您必须尝试创建一个 bean,该 bean 应该具有用于数据而不是集合的 getter 和 setter,并且集合应该在 bean 中定义。您可以在创建线程对象之前创建 bean 对象,并在构造时将 bean 对象传递给线程,希望这对您有用。
In my assumption you are directly accessing the collection (Any Fifo based), you must try to make a bean which should have getter and setters for data not for collection and the collection should be define in bean. you can create the bean object before you create thread objects and pass the bean object to threads at contructing time, hope this will you.