I have big problem with understanding multi threading in my application and because of that finding a bug. I've checked I think all possibilities and still I am getting various (sometimes unexpected) errors.
Maybe someone here will be able to advice me, what I should do.
In my project I am using two external libraries:
- GraphView - provides views for graph drawing
- EventBus - provides interface for easy communication between app components
As for the app it has the structure like this:
MainActivity
/ \
/ \
Thread Fragment
(ProcessThread) (GraphFragment)
The idea is that ProcessThread
computes data and provides constant stream of values to GraphFragment
throught EventBus
. In GraphFragment
I have one Series
required by GraphView
.
To update graphs in real time according to the example I need to make a new Runnable
so I did one:
private class PlotsRun implements Runnable{
@Override
public void run() {
mSeries1.appendData(new DataPoint(counter, getRandom()), true, 100);
counter++;
mHandler.post(this);
}
}
and when I start it from fragment's onResume()
method everything is working like a charm.
Unfortunately as I've mentioned I am using external data from another thread. To get it inGraphFragment
I am using (according to the documentation) onEventMainThread()
method.
And in here no matter what I'll do I can't pass data to update my graph in PlotsRun
object. So far I've tried:
- using
Queue
- add value inonEventMainThread
and get inPlotsRun
. It turned out that runnable is reading faster than method is able to update queue. - creating various buffers - the result is quite this same as with
Queue
. - calling
mSeries1.appendData(new DataPoint(counter, getRandom()), true, 100);
directly fromonEventMainThread
- at some point it gets freez. - creating
onEvent()
method inside my runnable and call from theremHandler.post()
- it is blocking UI and updates looks like snapshots. - using everything mentioned with or without
synchronized()
block.
What is quite difficult for me to understand is this runnable which is working correctly (at some point).
As it is said on official Android blog you can't update UI from non UI thread. This is why I can't use another thread inside GraphFragment
. But when I've checked my runnable it is running on main thread (UI). This is why I can't create infinite while loop
there instead have to call mHandler.post(this)
.
And still it behaves like another thread because it is faster (called more frequently) then onEventMainThread
method.
What can I do to be able to update my graphs (or where I should look) using data from ProcessThread
?
EDIT1:
Answering on @Matt Wolfe request I am including what I think is the most important part of a code for this problem with all required variable shown how they are declared. It is very simplified example:
MainActivity
:
private ProcessThread testThread = new ProcessThread();
@Override
protected void onResume() {
super.onResume();
testThread.start();
}
private class ProcessThread extends Thread{
private float value = 0f;
private ReadingsUpdateData updater = new ReadingsUpdateData(values);
public void run() {
while(true) {
value = getRandom();
updater.setData(value);
EventBus.getDefault().post(updater);
}
}
}
GraphFragment
:
private LineGraphSeries<DataPoint> mSeries1;
long counter = 0;
private Queue<ReadingsUpdateData> queue;
@Override
public void onResume() {
super.onResume();
mTimer2.run();
}
public void onEventMainThread(ReadingsUpdateData data){
synchronized(queue){
queue.add(data);
}
}
private class PlotsRun implements Runnable{
@Override
public void run() {
if (queue.size()>0) {
mSeries1.appendData(new DataPoint(counter, queue.poll()), true, 100);
counter++;
}
mHandler.post(this);
}
}
The if in runnable is added for protection because of this to fast reading problem. But it shouldn't be here because there should always be something (at least I expect that).
One more thing to add - when I put simple Log.d
and counting variable inside onEventMainThread
it was updating and displaying it's value correctly, but unfortunately logcat isn't main UI.
EDIT2:
This is mainly response for @MattWolfe comment
The mHandler is just variable declared and created in GrapgFragment:
private final Handler mHandler = new Handler();
private Runnable mTimer2;
Yes, that is right I am using mHandler.post()
without any delay. I'll try using some delay to see if there is any difference.
What I didn't mention earlier is that the ProcessThread
is providing also data to other fragments - don't worry they don't interfere with each other or share any resources. This is why I am using EventBus
.
EDIT3:
This a code that I've used as my another idea with another thread in GraphFragment
and runOnMainThread
method:
private MyThread thread = new MyThread();
private class MyThread extends Thread {
Queue<ReadingsUpdateData> inputList;
ReadingsUpdateData msg;
public MyThread() {
inputList = new LinkedList<>();
}
public void run() {
while(true) {
try{
msg = inputList.poll();
} catch(NoSuchElementException nse){
continue;
}
if (msg == null) {
continue;
}
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mSeries1.appendData(new DataPoint(counter, getRandom()), true, 100);
counter++;
}
});
}
}
public void onEvent(ReadingsUpdateData data){
inputList.add(data);
}
}
Unfortunately, it isn't working neither.