【发布时间】:2014-07-13 03:46:55
【问题描述】:
我遇到了似乎是僵局的情况。死锁听起来有点像:
- 无法关闭窗口
- 如果没有 IDE 上的终止按钮,则无法终止
- 空白,没有任何反应,完全没有异常或错误。
如果这些都是在死锁中发生的事情,那么我可能已经解决了一半的问题。我知道有两个线程正在运行:AWT-EventQueue-0 和 frameThread。
这是使用我构建但尚未完全开发的自定义库(您可能称之为 alpha-beta 阶段?)。我决定用它来制作乒乓球游戏。其实我的导师给我安排了这个游戏。我只是打算使用我的图书馆。
我的库使用 Swing 组件,我怀疑这与它有什么关系。
我想指出,intrinsic locks according to the oracle tutorials 声明
“当线程调用同步方法时,它会自动获取该方法对象的内在锁,并在方法返回时释放它。即使返回是由未捕获的异常引起的,也会释放锁。”
在执行此操作之前,我已经完成了一个同步块以从我所知道的程序中唯一可以拥有锁的线程获取锁。失败的。所以我使方法同步,并且上面列出的要点发生了。
我的代码是
// Threads
static ThreadManager tm = new ThreadManager() {
@Override
protected void runFrameThread() {//ThreadManager has threads in it that you can start.
while (true) { //These are just the abstract inherited methods the
Main.jpane.repaint(); // threads inside the manager call
}
}
@Override
protected void runMathThread() {
}
@Override
protected void runIntenseMathThread() {
}
};
// set frame rate
static {
tm.setFPS(30L);
}
public synchronized void draw(Graphics g) {// main problem: synchronized method here.
try {
wait(hertz);
} catch (InterruptedException e) {
System.err.println("ERROR: " + e.getLocalizedMessage());
e.printStackTrace();
}
g.setColor(rgb);
g.fillRect(this.x, this.y, width, height);
}
如果这没有帮助,您可以尝试查看我的代码...
My Code Repository for the Pong game
我最好的选择是我为延迟该方法所做的事情有问题。我想要做的是以“x”hertz 的设定速率为每个对象设置更新速率。如果它是一个返回类型的方法(不是 void)会更容易。
【问题讨论】:
标签: java multithreading swing