1

Further to my post Android: How to avoid errors on unlockCanvasAndPost method? I faced with a strange behaviour. I looked at the shared variable to use with the different threads. And by being happy I have used the solution proposed by @user1031431 in the focused-on-Java post Sharing a variable between multiple different threads. But nothing occured and the new values of the variable are stayed invisible for any non-main thread.

I tried the following:

public class MyMainThread extends WallpaperService{
    ....
    class MyEngine extends Engine implements OnSharedPreferenceChangeListener{
        ...
        private final Paint mPaint = new Paint();
        private final GestureDetector doubleTap;
        ...
        private MyThread myThread = null;
        ...
        WallpaperEngine() {
            ....
        doubleTap = new GestureDetector(MyMainThread.this,
                                            new SimpleOnGestureListener() {
                @Override
                public boolean onDoubleTap(MotionEvent e) {
                    if (mTouchEvents) {
                        if(myThread!=null){
                            control.myStopAnimationFlag = 1;
                        }
                        return true;
                    }
                    return false;
                    }
            });
            ....
        }
        ...
        class Control {
            public volatile int myStopAnimationFlag = 0;
        }
        final Control control = new Control();
        ...
        onSurfaceCreated(SurfaceHolder holder)
        {
            ...
            MyThread myThread = new MyThread(holder, mPaint);
            myThread.start();
            ...
        }
        ...
        class MyThread extends Thread{
            private SurfaceHolder mHolder;
            private Paint _paint;
            ...
            private final Handler mThreadHandler = new Handler(){
                public void handleMessage(android.os.Message msg){ };
            };

            private final Runnable mThreadWorker = new Runnable() {
                public void run() {
                    if (mDuration > 0)
                    {
                        control.myStopAnimationFlag = 0;
                        DrawFunction();
                    }
                }
            };

            void MyThread(SurfaceHolder holder, Paint paint){
                mHolder = holder;
                _paint = paint;
            }
            run(){
                DrawFunction();
            }
            ...
            DrawFunction(){
                ...
                StartAnimation();
                ...
                mThreadHandler.removeCallbacks(mThreadWorker);
                if (mVisible) {
                    mThreadHandler.postDelayed(mThreadWorker, 1000 / 2);
                }
            }
            StartAnimation(){
                ...
                while(...){
                    if(myStopAnimationFlag==0){
                        // draw algorithm here
                    }
                    else break;
                }
                ...
            }
        }
    }
    ....
}

In this code the lines with setting and checking the variable myStopAnimationFlag work unexpectedly. For example, if I produce the double tap during the animation (proceeded by cycle while(...)) the variable myStopAnimationFlag still is assigned to love and in double tap event handler I set it to 1. But I expect that after changing this variable in main thread it will be done for another thread too.

Also I tried to define a variable myStopAnimationFlag as static. But that nothing gave me again.

So I wait for advice to make it comes true.

Thanks in advance for giving me chance:)

5
  • did you check the instance of object control is same in accessing within thread and main activity Commented Nov 24, 2013 at 14:47
  • @Arju I checked it now. They are identical. But the next what I observed was that double tab event handler reached just after the animation was finished, even if I tab during the animation process. But these actions definately are executed in the different threads. I am in the big prostration. Commented Nov 24, 2013 at 16:52
  • If so you should sychronize the threads Commented Nov 24, 2013 at 16:59
  • 1
    @Arju, Thank you for answer. After long trip and no less the long holidays I should summarize all I achieved during this period. But not so much what I expected on. I tried to synchronize the thread, then I tried replace Thread with AsyncTask. And what I noticed is that the "while" cycle during processing does not catch any modifications on the sync. var. (for Thread) and on the public var. (for AsyncTask). However, if I use Runnable+Hanler instead "while" the var. is caught with proper values. But still I need do that with "while". Could you or anyone else give me any advice on this point? Commented Jan 8, 2014 at 14:45
  • possible duplicate of Sharing a variable between multiple different threads Commented Oct 5, 2014 at 1:53

1 Answer 1

0

I have shown two separate threads where second thread send some data to first thread using Handler

Class FirstThread extends Thread{
    int data;
    new Handler(){
    public void handleMessage(Message msg){
      super.handleMessage(msg);
      data = msg.arg1;
      doSomethingWithTheData(data);
    }
}

Class SecondThread extends Thread{
    int data = 5;
    Handler handlerOfTheFirstThread;
    SecondHandler(Handler handler){
    //The Second Thread needs reference to the handler of the firat thread to send data  
    handlerOfTheFirstThread = handler;
    }
    Message msg = Message.obtain(); // Need a Message object to contain data which Handlers would use to send data across Threads
    msg.arg1 = data;
    handlerOfTheFirstThread.sendMessage(msg);

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.