线程启动/停止在android上无法正常工作

时间:2012-12-13 17:48:57

标签: android multithreading restart

我需要一些帮助来启动/阻止我的怪物 我们这样生成:

for (int i = 0; i < 20; i++) {
    boolean notFree = true;
    int x = 0, y = 0;
    // while not free change
    // not determinstisch setting!
    while (notFree) {
        x = (int) (Math.random() * Config.XSIZE);
        y = (int) (Math.random() * Config.YSIZE);
        if (map.getMapPosition(x, y) == Config.EMPTYPOSITION) {
            notFree = false;
        }
    }
    switch ((int) Math.floor(Math.random() * 2)) {
    case 0:
        monsterVerktor.add(new Monster(context, x, y, this.map, 1,
                this.charac));
        break;
    case 1:
        monsterVerktor.add(new DeathReaper(context, x, y, this.map, 1,
                this.charac));
        break;
    default:
        break;
    }
}

我就这样阻止他们:(开始与Startmoving一样......)

public void stopAllMonsters() {
    for (Monster monster : monsterVerktor) {
        monster.getControl().stopMovingThread();
    }
 }

thread.stopMovingThread的工作方式如下:

public void stopMovingThread() {
    this.monster.setAlife(false);
    running = false;
    moveDone = true;
    boolean retry = true;
    while (retry) {
        try {
            thread.join();
            retry = false;
        } catch (InterruptedException e) {
            // try again shutting down the thread
        }
    }
}

到run():

public void startMovementThread() {
    running = true;
    thread = new Thread() {

        @Override
        public void run() {
            while (running) {
                Log.d(TAG, "run Thread");
                // get a little randomness in the movement ;)
                try {
                    // up to 0.5s sleeping till next moving
                    sleep(new Random().nextInt(Config.RANDOMSLEEP));
                } catch (InterruptedException e1) {}
                while (monster.isAlife()) {// check if alife
                    moveDone = false; // resett movement Done
                    noMove = false;
                    // Charakter in near if one is in near he
                    // will move to it.
                    if (!checkCharInAggroRange()) {
                        noMove = rndMove(); // if we have no move
                    }
                    while (!moveDone) {
                        timeBegin = System.currentTimeMillis();
                        // if hes not done with the move
                        if (monster.moveToX == monster.positionX
                            && monster.moveToY == monster.positionY) {
                            if (noMove) {
                                try {// sleep because no movement with a bit
                                        // randomness
                                    sleep(Config.MONSTERMOVINGTIME
                                        + (new Random()
                                                .nextInt(Config.RANDOMSLEEP)));
                                } catch (InterruptedException e) {
                                    Log.d(TAG,
                                        "An error occured during sleep because"
                                            + " of no Animatino");
                                }
                            }
                            // false after sleep
                            moveDone = false;
                            break; // already at the right position!
                            // movetoX is left of position
                        } else if (monster.moveToX > monster.positionX) {
                            monster.positionX++;
                            // movetoX is left of it
                        } else if (monster.moveToX < monster.positionX) {
                            monster.positionX--;
                            // movetoY is left of position
                        } else if (monster.moveToY > monster.positionY) {
                            monster.positionY++;
                            // movetoY is left of position
                        } else if (monster.moveToY < monster.positionY) {
                            monster.positionY--;
                        }
                        // sleep if hes moving to fast!
                        timeDiff = System.currentTimeMillis() - timeBegin;
                        sleepTimer = (int) (Config.MONSTERTIMING - timeDiff);
                        if (sleepTimer > 0) { // if >0 we are fast enough
                            // and can sleep a bit ;)
                            try {
                                sleep(sleepTimer);
                            } catch (InterruptedException e) {
                                Log.d(TAG, "Gameloop thread cant sleep");
                            }
                        }
                    }
                }
                try {
                    sleep(Config.RESPAWNTIMER);
                } catch (InterruptedException e) {
                    Log.d(TAG, "Monsterthread cant sleep");
                }
                // respawn it after a sleep :)
                respawn();
            }
        }
    };
    thread.start();
}

如果我们想停止它们并像这样启动它们,Android就会停止工作,我不明白为什么。我们也像这样停止/开始我们的渲染。 (嗯,这是一个可运行的,这不是)

Monster isAlife等:

public class Monster extends Drawable {

    private static final String TAG = Monster.class.getSimpleName();
    private int fullLife; // full life
    private int curLife; // current life
    public Context context;
    private Character charac;
    // ..
    // Basic monsterstuff
    private boolean alife, moveDone;
    private int level;
    public Status status;
    private MonsterControl control;

    // ..
    public Monster(Context context, int mapPosX, int mapPosY, Map map,
            int level, Character cha) {
        this.context = context;
        this.map = map;
        this.setCharac(cha);
        this.mapPosition[0] = mapPosX;
        this.mapPosition[1] = mapPosY;
        this.status = Status.IDLE;
        // example for full life calculation
        this.fullLife = level * 100 + ((int) (Math.random() * 10 * level)); // Examples
        this.curLife = this.fullLife;
        this.map.setMapPosition(mapPosX, mapPosY, Config.MONSTERSTATE);
        // set monster position
        // ..
        // load the sprite bitmap
        // ...
        // Change this later!
        alife = true;
        Log.d(TAG, "Monster created");
        // Starting the Controler
        control = new MonsterControl(this, this.charac);
        control.startMovementThread();
        Log.d(TAG, "Monster start moving");
        // exemplarisch cut of the sprite
        this.monsterPicAnimation();
    }
}

这里是aLife的getter / setter

public boolean isAlife() {
    return alife;
}

public void setAlife(boolean alife) {
    this.alife = alife;
}

3 个答案:

答案 0 :(得分:3)

首先,您对“Android停止工作”的评论,您是否暗示崩溃?有一个LogCat跟踪?

除此之外,这种分析是否正确?

1.创造一个怪物。

running = true;
isAlife = true; //or whatever monster.isAlife() checks

2.Sread开始。

while (running) {
  ...
  while (monster.isAlife()) {
    ...
  }
}

3.您是否尝试通过(1)将运行设置为false以及(2)加入线程来停止线程?


假设这一切都是真的。怪物线程仍然运行,直到从嵌套的while(monster.isAlife())循环中踢出它。一旦结束,while(running)将评估为false并且线程应该终止,继续前进到集合中的下一个怪物。如果没有这个终止,你将等待每个怪物死亡,同时用join()锁定主线程。

答案 1 :(得分:2)

除了我没有详细研究的代码逻辑外,共享变量之间缺乏同步。特别是,您的代码不能保证您的线程(在startMovementThread中)能够观察到主线程对您的标志所做的更改。

良好的第一步是使这些变量变得不稳定:

private volatile boolean alife, moveDone;

但请注意,可能存在其他问题(取决于您的要求)。例如,当这些语句在startMovementThread中执行时:

if (monster.moveToX == monster.positionX ...)
monster.positionX++;

在该主题中可能看不到该主题中所做的任何更改,反之亦然。如果这可能是一个问题,您也需要同步对这些变量的访问(并使它们变得不稳定,因为例如monster.positionX++不是原子的)。

答案 2 :(得分:0)

我自己用“好”的想法解决了这个问题。 问题是,我让他们在某些时候睡觉。如果他们睡觉就不能停止/加入线程,甚至不要看它是否“正在运行”。所以我用一个看起来像这样的小方法解决了它:

private void breakableSleep(int i, int intervall){
    int x = 0;
    while(running && x <= i){
        try {
            this.thread.sleep(intervall);
        } catch (InterruptedException e) {
            Log.d(TAG, "Monsterthread cant sleep");
        }
        x++;
    }
}

它可以在每个intervall停止。当然它会花费一些电池,但没有找到任何其他方式来以“快速”的方式阻止怪物,因为如果他们死了,一些人睡了大约3秒钟(重生) 我还重写了整个startMovementThread,因为在我看来它并不好看。

最好的问候