gpt4 book ai didi

Java 线程暂停后无法恢复

转载 作者:行者123 更新时间:2023-12-01 11:57:19 24 4
gpt4 key购买 nike

首先,我知道有几个关于我的问题的主题,并且我已经阅读了很多...我发现的最好的来自 Java Docu ( link ),我尝试调整他们的解决方案。

但不幸的是,我的生活小游戏程序没有按预期工作...当我暂停模拟游戏的线程(在 JPanel 上绘制下一代)时,它会按预期停止,当我尝试恢复时,什么也没有发生(没有异常(exception),没有机会调试)。我当然可以做的是,完全停止线程并创建一个新线程,然后充当暂停/恢复...我不想使用该解决方法,因为我想了解我创建的问题,其次我想以不同的方式使用启动线程;)

这是生成线程的代码(我主要使用 volatile 和 synchronized):

public class MainJFrame extends JFrame {

private static final long serialVersionUID = 1L;
private JPanelGrid jPanelGrid;
private volatile Thread simulation;
private volatile boolean threadSuspended;

private JMenuBar menuBar;
private JMenu main;

public MainJFrame() {
jPanelGrid = new JPanelGrid(995, 770);
getContentPane().add(jPanelGrid);
initMenuBar();
setJMenuBar(menuBar);
pack();

setTitle("Conrads Game of Life");
setSize(1000, 800);
setLocationRelativeTo(null); // displayed in the middle of the screen
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}

public void startSimulation() {
//jPanelGrid.reset();
threadSuspended = false;
simulation = new Thread() {
@Override
public void run() {
Thread thisThread = Thread.currentThread();
while (simulation == thisThread) { //this check is for the correct stop of the thread
try {
Thread.sleep(50);
if(threadSuspended){ //this additional if is a performance tweak regarding Java Docu
synchronized (this) { //this part should be for the pause and resume feature
while (threadSuspended) {
wait();
}
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
jPanelGrid.paintNextGen(); //Do I access it incorrectly?
}
}
};
simulation.start();
}

public synchronized void stopSimulation() {
simulation = null;
notifyAll();
}

public synchronized void pauseResume(){
threadSuspended = !threadSuspended;
if(!threadSuspended){
notifyAll();
}
}

private void initMenuBar(){
menuBar = new JMenuBar();
main = new JMenu("Main");
menuBar.add(main);

JMenuItem pauseResume = new JMenuItem("Pause/Resume");
pauseResume.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
pauseResume();
}

});
main.add(pauseResume);

JMenuItem start = new JMenuItem("Start");
start.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
startSimulation();
}

});
main.add(start);

JMenuItem stop = new JMenuItem("Stop");
stop.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
stopSimulation();
}

});
main.add(stop);
}

}

最佳答案

主要问题

您正在不同线程之间共享threadSuspished变量。您通过同步来保护它,但是您正在不同的对象上进行同步。

synchronized (this) <-- 这里 this 是一个 Thread 对象,尝试使用 JFrame.this 改为

public Synchronized void PauseResume(){ <-- 这里您正在 JFrame 对象上进行同步

<小时/>

编辑:删除了有关双重检查的部分,您正在使用 volatile ,所以在Java 5.0之后,我认为这段代码应该没问题。请参阅https://en.wikipedia.org/wiki/Double-checked_locking了解更多信息。

关于Java 线程暂停后无法恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28344267/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com