我正在创建一个可以从文件加载关卡的游戏。这是在一个单独的线程上完成的,而其他一切都是在事件调度线程上完成的。
我通过从一个非常大的测试文件加载来测试代码,结果发现事件调度线程有时在加载关卡时没有响应。
我似乎无法找出原因。这是我的一些代码:
public class LevelSelectionWrapper extends GamePanel {
...
private JList list;
private File[] files;
...
//Lock object for synchronization
private Object lock = new Object();
//Runnable for loading levels from files on a separate thread
private Runnable loader = new Runnable() {
@Override
public void run() {
synchronized(lock) {
//Load levels from files
List<Level> levels = LevelLoader.load(files); // <-------------
...
SwingUtilities.invokeLater(new ListUpdater());
}
}
};
...
private void createOpenFileButton(Container container) {
final JFileChooser fc = ...
...
//Create open button
JButton openButton = new JButton("Open file");
openButton.setFocusable(false);
openButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int returnVal = fc.showOpenDialog(LevelSelectionWrapper.this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
synchronized(lock) { files = fc.getSelectedFiles(); }
//Load files on separate thread
new Thread(loader).start(); // <-------------
}
}
});
container.add(openButton);
}
}
我在代码中添加了两个箭头:
- 第一个是耗时的方法(当文件非常大时)。有时,事件调度线程在此代码运行时无响应。
- 最后一个是调用runnable的地方。