1

我有 2 个活动:A 和 B。第一个 (A) 创建并启动 3 个线程。

当用户插入第二个活动(B)时,我需要暂停所有线程,直到用户返回到 A 活动。

我尝试用 onPause() 和 onResume() 来做,但它似乎不好,也不起作用:(在活动 a)

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

编辑:以下建议的解决方案之后的运行方法之一:

@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();
            }
        }
    }
4

2 回答 2

2

这是一个真正糟糕的设计。如果您在 Activity 中启动一个线程并在 onStop() 上停止它,那么该 Activity 会被破坏(并且您的线程也会被破坏)但更糟糕的情况是:无法在后台破坏 Activity - 大量内存泄漏 - 你会得到一项新活动,但可以取消引用旧活动。

在这种用例中使用服务会更好

于 2013-10-23T14:48:15.447 回答
0

当你调用它时,你是shop.wait()从主线程调用的onPause。您必须从要停止的线程中调用它。首先,不要将Const.isWaiting其用作公共变量,而是将其设为私有并为其提供同步的 getter 和 setter(例如isWaiting()and setWaiting(boolean waiting))。(不过,这一步可能是不必要的,因为我认为单个布尔值已经是线程安全的。)

然后:

@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();
    }
}
于 2013-10-21T15:15:44.263 回答