繁体   English   中英

同步方法中的Java线程安全错误

[英]java thread safety error in synchronized method

我有这堂课

package net.omnosis.mazegame.components;

import net.omnosis.mazegame.SlicedBitmap;
import android.graphics.Bitmap;

public class PlayerLayer extends DrawableLayer {

    private Player player;
    private XY tileImageSize;

    private int[] move = new int[] { 1, 2, 3, 4, 3, 2, 1, 6, 7, 8, 7, 6 };
    //private int[] move = new int[] { 8 };

    private int moveCount;
    private int moveCountMax = move.length;

    private Bitmap playerBitmap;
    public SlicedBitmap playerTiles;

    private int line;

    private static final int VERTICAL = 0;
    private static final int HORIZONTAL = 8;

    public PlayerLayer(Player player, Bitmap playerBitmap, XY tileImageSize) {

        this.playerBitmap = playerBitmap;
        this.tileImageSize = tileImageSize;
        playerTiles = new SlicedBitmap(playerBitmap, tileImageSize.x(), tileImageSize.y());

        setPlayer(player);

        update();
    }

    public final void setPlayer(Player player) {
        if (this.player != null) {
            this.player.removeListener(this);
        }

        this.player = player;
        player.addListener(this);
        update();
    }

    public void updateDirection() {
        Direction dir = player.getHeading();

        if (dir == Direction.LEFT || dir == Direction.RIGHT) {
            line = HORIZONTAL;
        } else if (dir == Direction.TOP || dir == Direction.BOTTOM) {
            line = VERTICAL;
        }
    }

    public synchronized void animate() {

        if (player.isMoving()) {

            moveCount++;

            if (moveCount >= moveCountMax) {

                player.finishMove();
                moveCount = 0;
            }
        } else {

        }

        updateDirection();
        super.update();
    }

    public void update() {
        updateDirection();
        super.update();
    }

    public XY getSpritePos() {
        XY playerPos = new XY(player.getCurrentPosition().x() * tileImageSize.x() + (tileImageSize.x() / 2), player.getCurrentPosition().y() * tileImageSize.y() + (tileImageSize.y() / 2));
        XY animationPos = getAnimationPos();
        return playerPos.add(animationPos);
    }

    public XY getAnimationPos() {
        double step = (double) tileImageSize.x() / moveCountMax * moveCount;
        return player.getHeading().multiply((int) step);
    }

    public Bitmap getBitmap() {

        if (moveCount >= moveCountMax) {
            System.out.println("BUG! MORE: " + moveCount + "  max: " + moveCountMax);
            moveCount = 0;
        }
        return playerTiles.getTile(move[moveCount] + line);
    }
}

线程每10毫秒调用一次animate方法。 有时我得到以下输出: BUG! MORE: 12 max: 12 BUG! MORE: 12 max: 12这是因为我在getBitmap()方法中再次检查了值。 为什么?

我不明白,如果动画是synchronized ,moveCount怎么会大于11?

如果模拟器滞后,则这种情况会更频繁地发生。

您将在已synchronized块中递增和重置moveCount ,但是当您在getBitmap()方法中访问moveCount变量时,您并未在同一锁上进行同步。

这意味着线程A可以在animate()方法的中间,并且将moveCount递增为与moveCountMax相等。 然后,线程B进入getBitmap()并读取moveCount的值, 然后线程A将moveCount重置为0。

通常,不仅需要在写入变量的值时同步,还需要在读取变量时(在同一锁上)进行同步,尤其是在对该变量进行的一项操作(例如animate()方法)时,尤其要进行同步。涉及复合操作(递增,然后可能重置为0)

顺便说一句,如果moveCountMax是一个常量值( = moves.length ),则将其标记为final

您需要同步对共享可变数据的所有访问。 您可以在增量上进行同步,这很好,但是您不能在getBitmap的读取上进行同步。 这意味着线程可以在递增或紧随其后读取getBitmap moveCount

假设您增加了moveCount并且在该递增线程将其设置为0之前,另一个线程调用了getBitmap,其中if (moveCount >= moveCountMax) {可以为true。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM