2013-11-24 150 views
1

繼我的帖子Android: How to avoid errors on unlockCanvasAndPost method?我面臨一個奇怪的行爲。我查看了用於不同線程的共享變量。通過感到高興,我使用了@ user1031431提出的解決方案,專注於Java post 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爲靜態。但是沒有再給我什麼。

所以我等待建議,使其成真。

在此先感謝給我機會:)

+0

你檢查的情況下一些數據發送到第一線的目標控制是相同的在線程和主要活動中訪問 –

+0

@Arju我現在檢查它。它們是相同的。但接下來我觀察到的是,在動畫結束之後,即使在動畫過程中我選擇了tab,雙tab選項事件處理程序也會到達。但是這些動作肯定會在不同的線程中執行。我處於大衰退之中。 –

+0

如果是這樣你應該同步線程 –

回答

0

我已經展示了兩個獨立的線程,其中第二個線程使用處理器

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

}