多个线程的共享变量

时间:2013-11-24 14:02:12

标签: android multithreading synchronization shared

继我的帖子Android: How to avoid errors on unlockCanvasAndPost method?之后,我遇到了一种奇怪的行为。我查看了共享变量以用于不同的线程。通过高兴,我使用了@ user1031431提出的解决方案,专注于Java Sharing a variable between multiple different threads。但是没有任何事情发生,并且变量的新值对于任何非主线程都保持不可见。

我尝试了以下内容:

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;
                }
                ...
            }
        }
    }
    ....
}

在此代码中,设置和检查变量 myStopAnimationFlag 的行意外地工作。例如,如果我在动画期间产生双击(按周期继续(...))变量 myStopAnimationFlag 仍然被分配给爱,并且在双击事件处理程序中我将其设置为1。但我希望在主线程中更改此变量之后,它也将在另一个线程中完成。

此外,我尝试将变量 myStopAnimationFlag 定义为静态。但是没有什么能再次给我了。

所以我等待建议让它成真。

提前感谢给我机会:)

1 个答案:

答案 0 :(得分:0)

我已经展示了两个独立的线程,其中第二个线程使用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);

}