2013-10-21 26 views
1

我有2 Activty:A和B 第(一)創建並啓動3個線程。機器人 - 暫停運行的線程,而另一項活動是在屏幕

當用戶插入第二個活動(B),我需要所有的線程將被暫停,直到用戶迴歸A活性。

我試着用的onPause()和的onResume()來做到這一點,但它似乎不是很好,也沒有工作:(在活動)

@Override 
public void onPause() 
{ 
    Const.isWainting = true; 
    super.onPause(); 
    try { 
     synchronized(Const.shop){ 
     Const.shop.wait(); 
     } 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

@Override 
public void onResume() 
{ 
    super.onResume(); 
    if(Const.isWainting == true){ 
     synchronized(Const.shop){ 
     Const.shop.notifyAll(); 
     Const.isWainting = false; 
     } 
    } 
} 

編輯:一個run方法的建議後,解決方案如下:

@Override 
public void run() 
{ 

    while (true) 
    { 

     if (this.isInterrupted() && Const.isWainting){ 
      synchronized (Const.shop) { 
       try { 
        Const.shop.wait(); 
        Const.isWainting = false; 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     } 


     for(int i=0; i<Const.MAX_FOOD ; i++){ 

      if(foodArr[i].getStatus() != Const.NOT_ACTIVE){ 

       //move the food one step    
       foodArr[i].move(); 


       if(foodArr[i].getY() > Const.screenHeight){ 
        foodArr[i].setStatus(Const.NOT_ACTIVE); 
       } 
       gameView.postInvalidate(); 
      } 

     } 

     if (isInterrupted() && Const.isWainting){ 
      synchronized (Const.shop) { 
       try { 
        Const.shop.wait(); 
        Const.isWainting = false; 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     } 

     try 
     { 
      FoodThread.sleep(5); 
     } 
     catch (InterruptedException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 

我知道線程犯規停止監守食品繼續下去,甚至「移動」在一個循環中只有一個像素 - >從屏幕上所有的食物即消失或移動到多下來...

請幫助,非常感謝!

編輯2: 一個活動:

@Override 
public void onPause() 
{ 
    super.onPause(); 
    Const.isWainting = true; 
    this.threadAnimal.interrupt(); 
    this.threadFood.interrupt(); 
    this.threadMoney.interrupt(); 

} 

@Override 
public void onResume() 
{ 
    super.onResume(); 
    Const.isWainting = false; 
    if(Const.isWainting == true){ 
     synchronized(Const.shop){ 

      Const.shop.notifyAll(); 

     } 
    } 

} 

一個線程:

@Override 
    public void run() 
    { 

     while (true) 
     { 

      if (this.isInterrupted() && Const.isWainting){ 
       synchronized (Const.shop) { 
        try { 
         Const.shop.wait(); 

        } catch (InterruptedException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 
      } 




      for(int i=0; i<Const.MAX_FOOD ; i++){ 

       if(foodArr[i].getStatus() != Const.NOT_ACTIVE){ 


        foodArr[i].move(); 


        if(foodArr[i].getY() > Const.screenHeight){ 
         foodArr[i].setStatus(Const.NOT_ACTIVE); 
        } 
        gameView.postInvalidate(); 
       } 

      } 

      if (isInterrupted() && Const.isWainting){ 
       synchronized (Const.shop) { 
        try { 
         Const.shop.wait(); 

        } catch (InterruptedException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 
      } 

      try 
      { 
       FoodThread.sleep(5); 
      } 
      catch (InterruptedException e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 
+0

什麼樣的對象是Const.shop?什麼是等待和notifyAll方法看起來像? – Tenfour04

+0

Const.shop來自Object類型。該方法是對象的方法 – user1932595

回答

0

您從主線程中調用shop.wait()當你調用它 。你必須從你想停止的線程中調用它。首先,而是採用Const.isWaiting作爲公共變量,將其變爲私有,並給它同步的getter和setter方法(例如isWaiting()setWaiting(boolean waiting))。 (這一步可能是unnessary,但是,因爲我覺得一個布爾值已經線程安全的。)

然後:

@Override 
public void onPause() 
{ 
    super.onPause(); 
    Const.setWaiting(true); 
    for (Thread t in myThreeThreads) { 
     try { 
      t.interrupt(); 
     } catch (SecurityException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

而且在每個那些線程,可以在任何環,他們正在執行,他們應該定期做:

if (isInterrupted() && Const.isWaiting()){ 
    synchronized (Const.shop) { 
     Const.shop.wait(); 
    } 
} 

或者,如果他們也在做非循環的東西,那麼在每次長時間運行之前執行上述操作。

編輯:我想你也應該更新你的onResume,以確保isWaiting變量先被取消。

@Override 
public void onResume() 
{ 
    super.onResume(); 
    Const.setWaiting(false); //Now if a thread is just now getting to an interrupt check, it can just skip it and keep going. 
    synchronized(Const.shop){ 
     Const.shop.notifyAll(); 
    } 
} 
+0

好吧,我試過了。這對我來說似乎是一個很好的解決方案,但它不起作用 - 線程正在運行..爲什麼會發生? – user1932595

+0

線程將繼續運行,直到它到達檢查'isInterrupted()'的點。如果你可以發佈你的一個線程的運行方法,它可能會有所幫助。 – Tenfour04

+0

不,我將編輯我的文章 – user1932595

2

這是一個真正糟糕的設計。如果你在一個Activity中啓動一個線程並停止它onStop(),那麼這個activity就會被銷燬(並且mabye對你的線程也是如此),但最糟糕的情況是:activity不能在後臺被銷燬 - 大量內存泄漏 - 你會得到一個新的活動,但舊的活動可以被解除引用。

它的方式更好地使用services對於這種使用情況

+0

這是真的......它非常複雜。我認爲他們必須將這個邏輯從onPause移動到打開其他活動的代碼,並在onDestroy方法中調用notifyAll,並添加一個表示活動生命的布爾值以替換每個線程中的while(true)。然後交叉手指。 – Tenfour04

相關問題