thanhnt
thanhnt

Reputation: 41

shared data for multi thread in java

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: enter image description here

Upvotes: 1

Views: 1926

Answers (3)

Asad Rasheed
Asad Rasheed

Reputation: 518

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.

Upvotes: 0

Paul Sanwald
Paul Sanwald

Reputation: 11329

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.

Upvotes: 1

Saurabh Gokhale
Saurabh Gokhale

Reputation: 46395

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.

Upvotes: 0

Related Questions