开发者

shared data for multi thread in java

开发者 https://www.devze.com 2023-03-13 06:34 出处:网络
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

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. C开发者_Python百科icurlar 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:

shared data for multi thread in 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.


I think, Synchronization is the solution for your problem.

Threads communicate primarily by sharing access to fields and the objects reference fields refer to. This form of communication is extremely efficient, but makes two kinds of errors possible: thread interference and memory consistency errors. The tool needed to prevent these errors is synchronization.

From the JavaDoc's BlockingQueue

BlockingQueue implementations are thread-safe. All queuing methods achieve their effects atomically using internal locks or other forms of concurrency control. However, the bulk Collection operations addAll, containsAll, retainAll and removeAll are not necessarily performed atomically unless specified otherwise in an implementation. So it is possible, for example, for addAll(c) to fail (throwing an exception) after adding only some of the elements in c.


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.

0

精彩评论

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