如何阻止一个线程修改另一个线程正在使用的数组?

ara*_*guy 6 java swing multithreading thread-safety

我有一个基本上是游戏的java程序.它有一个名为"世界"的类 ."World"类有一个方法'levelChanger()',另一个方法'makeColorArray()'.

public class World {

    private BufferedImage map, map1, map2, map3;
    private Color[][] colorArray;

    public World(int scrWd, int scrHi) {
        try {
            map1 = ImageIO.read(new File("map1.png"));
            map2 = ImageIO.read(new File("map2.png"));
            map3 = ImageIO.read(new File("map3.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        map = map1;

        makeColorArray();
    }

    private void makeColorArray() {
        colorArray = new Color[mapHi][mapWd]; // resetting the color-array
        for(int i = 0; i < mapHi; i++) {
            for(int j = 0; j < mapWd; j++) {
                colorArray[i][j] = new Color(map.getRGB(j, i));
            }
        }
    }

    //color-array used by paint to paint the world
    public void paint(Graphics2D g2d, float camX, float camY) {
        for(int i = 0; i < mapHi; i++) {
            for(int j = 0; j < mapWd; j++) {
                if(colorArray[i][j].getRed() == 38 && colorArray[i][j].getGreen() == 127 && colorArray[i][j].getBlue() == 0) {
                    //draw Image 1
                }
                else if(colorArray[i][j].getRed() == 255 && colorArray[i][j].getGreen() == 0 && colorArray[i][j].getBlue() == 0) {
                    //draw Image 2
                }
            }
        }
    }

    public void levelChanger(Player player, Enemies enemies) {
        if(player.getRec().intersects(checkPoint[0])) {
            map = map2;
            //calls the color-array maker
            makeColorArray();           
        }
        else if(player.getRec().intersects(checkPoint[1])) {
            map = map3;
            makeColorArray();
        }
    }

    public void update(Player player, Enemies enemies) {
        levelChanger(player, enemies);
    }   
}
Run Code Online (Sandbox Code Playgroud)

makeColorArray()方法生成一个类型为'Color'的二维数组.此数组存储PNG图像中的颜色对象.JPanel的paint()方法使用此数组在屏幕上绘制世界.

levelChanger()方法用于在某些编码为真时更改游戏的级别(阶段).这是调用makeColorArray()方法在改变游戏级别时重新制作颜色数组的方法.

问题是我有一个在线程上运行的游戏循环.现在,像JPanel这样的swing组件的绘制是由java在不同的后台线程上完成的.当改变游戏等级时,重新制作颜色数组对象.现在,就像我之前所说的那样,paint()方法使用color-array对象在屏幕上绘制世界.有时,当根据游戏逻辑重新制作颜色数组对象并将其成员设置为null时,背景线程(未完成绘制)仍然使用颜色数组对象.这有时会导致空指针异常.显然是竞争条件.

我想知道如何阻止我的游戏线程重置颜色数组直到背景摆动线程完成绘画.

Hov*_*els 1

建议:

  • 对您的程序使用模型-视图-控制设计,或使用许多类似变体之一。
  • 使用 Swing 计时器来驱动 GUI 游戏循环,但在模型中使用实时切片(而不是计时器的延迟)来确定循环步骤之间的时间长度。
  • 该模型应在 GUI Swing 事件线程中运行。
  • 但其长时间运行的任务应该使用 SwingWorker 在后台线程中运行。
  • 这是关键:在后台线程完成处理之前,不要更新模型的数据(JPanel 用于绘制的数据)。PropertyChangeListener 和 SwingPropertyChangeSupport 对此很有用。
  • 确保 JPanel 绘制的是它的paintComponent(...)方法,而不是它的paint(...)方法,并且您调用的是 super 方法。
  • 最好将背景图像设为 BufferedImage,并让 JPanel 在其paintComponent(...)方法中绘制该图像以提高效率。
  • 确保所有 Swing GUI 代码(可能除外repaint())都在 Swing 事件线程上调用。
  • 是的,一定要阅读Swing 中的并发教程