Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ArrayList同步需要帮助_Java_Multithreading_Arraylist_Thread Safety - Fatal编程技术网

Java ArrayList同步需要帮助

Java ArrayList同步需要帮助,java,multithreading,arraylist,thread-safety,Java,Multithreading,Arraylist,Thread Safety,我正在尝试制作一个基于瓷砖的游戏。我制作了一个平铺类,并为每个平铺提供了自己的矩形,以便于碰撞。渲染和更新它们之后,我会将它们全部存储到ArrayList中。这部分工作正常,但当我试图通过getter从另一个类访问相同的ArrayList时,我没有得到任何元素。我用控制台检查大小,它给了我正确的数字,但是当我尝试实际获取一个元素时 i、 e.rect.get(0),我得到以下错误: 线程“thread-3”java.lang.IndexOutOfBoundsException中的异常:索引:0,

我正在尝试制作一个基于瓷砖的游戏。我制作了一个
平铺
类,并为每个平铺提供了自己的
矩形
,以便于碰撞。渲染和更新它们之后,我会将它们全部存储到
ArrayList
中。这部分工作正常,但当我试图通过getter从另一个类访问相同的
ArrayList
时,我没有得到任何元素。我用控制台检查大小,它给了我正确的数字,但是当我尝试实际获取一个元素时 i、 e.
rect.get(0)
,我得到以下错误:

线程“thread-3”java.lang.IndexOutOfBoundsException中的异常:索引:0,大小:0 位于java.util.ArrayList.rangeCheck(未知源) 位于java.util.ArrayList.get(未知源) 我认为这是一个线程安全问题,但我已经尝试了所有我能想到的方法,从使用
synchronize
关键字到
Collections
,但没有成功。任何帮助都将不胜感激

用代码编辑。很抱歉没有早点发布

public class Launcher extends JFrame implements Runnable {

    private static final long serialVersionUID = 1L;

    private static final int WIDTH = 1500;
    private static final int HEIGHT = 900;

    private static final String title = ("Game Alpha");

    volatile boolean running;

    Thread CT;

    Canvas canvas = new Canvas();
    Dimension size = new Dimension(WIDTH, HEIGHT);

    Camera camera;

    Controls controls;

    PlayerTest pt;

    BufferStrategy BS;

    Graphics graphics;

    //I'm declaring the arrayList here
    public  ArrayList<Rectangle> bounded = new ArrayList<Rectangle>();

    public Launcher() {

        Sprites.resources();
        controls = new Controls();
        GUI();
        //The camera class is the one in which I'm trying to access the list
        //It's there right now just to test, I plan on changing it later
        camera = new Camera(this, controls, 0, 0);

        pt = new PlayerTest(this, canvas.getWidth() / 2, canvas.getHeight() / 2);

        Tiles.tileSets();

        CT = new Thread(this);
        START();

    }

    public void GUI() {
        setTitle(title);
        setPreferredSize(size);
        setResizable(false);
        pack();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        addKeyListener(controls);

        canvas.setPreferredSize(size);
        canvas.setMaximumSize(size);
        canvas.setMinimumSize(size);
        canvas.setFocusable(false);
        add(canvas);

        setVisible(true);
    }

    public void render() {
        BS = canvas.getBufferStrategy();

        if(BS == null) {
            canvas.createBufferStrategy(3);
            return;
        }


        graphics = BS.getDrawGraphics();
        graphics.setColor(Color.ORANGE);

        graphics.fillRect(0, 0, WIDTH, HEIGHT);

        Rectangle cam = new Rectangle((int)camera.getX(), 
                (int)camera.getY(), 
                camera.getWidth(), 
                camera.getHeight());

        //Here I'm looping to get my tiles
        for(int x = 0; x < 64; x++) {
            for(int y = 0; y < 64; y++) {
                Tiles.getTile(LevelCode.code[y][x]).setBounds( x * 50 - (int)camera.getXof(),  y * 50 - (int)camera.getYof());

                //Here I'm adding the rectangles from each tile into the array
                bounded.add(new Rectangle(Tiles.getTile(code[y][x]).getBounds()));

            }   
        }

        pt.render(graphics);

        BS.show();
        graphics.dispose();
//Here after I render the tiles I clear the list, for my updates, then //repopulate
        bounded.clear();

    }

    public ArrayList<Rectangle> getList() {
        return bounded;
    }

}
公共类启动器扩展JFrame实现可运行{
私有静态最终长serialVersionUID=1L;
专用静态最终整数宽度=1500;
专用静态最终内部高度=900;
私有静态最终字符串标题=(“游戏Alpha”);
易失布尔运行;
螺纹CT;
画布=新画布();
尺寸尺寸=新尺寸(宽度、高度);
摄像机;
控制;
PlayerTest pt;
缓冲策略;
图形;
//我在这里申报arrayList
public ArrayList bounded=新的ArrayList();
公共启动器(){
Sprites.resources();
控件=新控件();
GUI();
//camera类是我试图访问列表的类
//它现在就在那里只是为了测试,我计划以后再更改它
摄影机=新摄影机(此,控件,0,0);
pt=newplayertest(这是canvas.getWidth()/2,canvas.getHeight()/2);
Tiles.tileSets();
CT=新螺纹(该螺纹);
START();
}
公共用户界面(){
片名(片名);
设置首选大小(大小);
可设置大小(假);
包装();
setDefaultCloseOperation(关闭时退出);
setLocationRelativeTo(空);
addKeyListener(控件);
canvas.setPreferredSize(大小);
canvas.setMaximumSize(大小);
canvas.setMinimumSize(大小);
canvas.setFocusable(false);
添加(画布);
setVisible(真);
}
公共无效呈现(){
BS=canvas.getBufferStrategy();
如果(BS==null){
canvas.createBufferStrategy(3);
返回;
}
graphics=BS.getDrawGraphics();
图形.设置颜色(颜色.橙色);
fillRect(0,0,宽度,高度);
矩形cam=新矩形((int)camera.getX(),
(int)camera.getY(),
camera.getWidth(),
camera.getHeight());
//在这里,我循环得到我的瓷砖
对于(int x=0;x<64;x++){
对于(int y=0;y<64;y++){
Tiles.getTile(LevelCode.code[y][x]).setBounds(x*50-(int)camera.getXof(),y*50-(int)camera.getYof());
//在这里,我将每个平铺中的矩形添加到数组中
add(新矩形(Tiles.getTile(代码[y][x]).getBounds());
}   
}
绘制(图形);
b.show();
graphics.dispose();
//在这里,在渲染平铺后,我清除列表以进行更新,然后//重新填充
有界。清除();
}
公共ArrayList getList(){
收益有界;
}
}
同步(如果操作正确)意味着这不是线程安全问题,尽管您尚未发布实际代码,因此可能是您做错了


这里最有可能的情况是,实际上有多个
ArrayList
s,而您访问的是错误的。尝试检查id(当您
切换到字符串时,#后面的数字)并确保两个位置的id相同。

我们无法调试看不到的代码。请构建一个可测试的最小示例。我认为这更多的是一个设计问题,而不是多线程问题。您可以使用关键字“synchronized”使方法线程保存。所以请插入一些代码示例。我打赌100分,他有两个列表,而他试图阅读的列表是空的。尝试格式化你的问题。整件事可能不应该是一个段落。在函数调用和错误消息上也使用代码块。