Unexpected behavior of same methods in different threads

走远了吗. 提交于 2019-12-25 16:55:08

问题


My initial question was: Android GraphView project get freeze with real time updates. In this one I was asking about possible concurrency in UI thread of 3 plots. On memory allocation plot it looks like this:

I was receiving data directly from my ProcessThread in main activity and pass it using onEventMainThread from EventBus library back to the GraphFragment. All the data that is passed comes from ProcessThread which gathers data from Bluetooth listening service and then proceeds it to get meaningful numbers.

My idea was to test if this same will happen with test thread that only generates data and sends it to onEventMainThread. Because this also produces some errors I was forced to ask another question: Difficulty in understanding complex multi threading in Android app. After some time I've received great answer from @AsifMujteba explaining that my test thread is simply too fast.

Knowing that I was able to return to my main problem and my real thread to check if all the timings are correct. As I've said there is a lot going on so being to fast is not a problem (however, I've added this same mechanize to test if data isn't send to fast). I would be more concern about to slow work of this thread.

My current onEventMainThread looks like that:

public void onEventMainThread(float[] data) {
            mSeries1.appendData(new DataPoint(counter,data[0]),true,100);
            mSeries1.appendData(new DataPoint(counter,data[1]),true,100);
            mSeries1.appendData(new DataPoint(counter,data[2]),true,100);
            counter++;
        }

Unfortunately when I've returned to the beginning the problem emerged again. After a lot of testing I am able to say that data looks like is being send correctly. I've checked it with two markers:

public void onEventMainThread(float[] data) {
                Log.d("LOG","marker1");
                mSeries1.appendData(new DataPoint(counter,data[0]),true,100);
                mSeries1.appendData(new DataPoint(counter,data[1]),true,100);
                mSeries1.appendData(new DataPoint(counter,data[2]),true,100);
                counter++;
                Log.d("LOG","marker2");
            }

Logcat messages are appearing correctly. Unfortunately the error appears even though the sending looks this same as in my test thread:

if((System.currentTimeMillis()-start)>10) {
    values[0] = (float) getRandom();
    values[1] = (float) getRandom();
    values[2] = (float) getRandom();
    EventBus.getDefault().post(values);
    start = System.currentTimeMillis();
}

What's more I am sure that the data is correctly send all the time because when I've tested another fragment with OpenGL visualization everything works.

So to sum everything up:

When sending values to the fragment using EventBus from one (very simple) thread everything works great, while sending from another (more complex) thread ends in freezing of display and showed memory allocation graph. It is important to know, that if one thread is running the second one is commented out.

Can someone please advice me what might be a problem here? Or what should I check more?

EDIT

I have done one more test with commenting out everything regarding Series data append leaving only Log.d() and no error appeared. What is interesting is that the blocking (or freezing) of graph updates doesn't affect UI itself so I can still press all the buttons and so on.


回答1:


Have you tried using a Custom eventbus and not the default one? I had a similiar problem today and i fixed that by creating a custom evenbus with a seperate ThreadPool and it worked like a charm.



来源:https://stackoverflow.com/questions/30458860/unexpected-behavior-of-same-methods-in-different-threads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!