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:)

Community
  • 1
  • 1
Sergey V.
  • 981
  • 2
  • 12
  • 24
  • did you check the instance of object control is same in accessing within thread and main activity – Viswanath Lekshmanan Nov 24 '13 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. – Sergey V. Nov 24 '13 at 16:52
  • If so you should sychronize the threads – Viswanath Lekshmanan Nov 24 '13 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? – Sergey V. Jan 08 '14 at 14:45
  • possible duplicate of [Sharing a variable between multiple different threads](http://stackoverflow.com/questions/13582395/sharing-a-variable-between-multiple-different-threads) – jww Oct 05 '14 at 01:53

1 Answers1

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);

}
Lazycoder-007
  • 1,055
  • 9
  • 19