【发布时间】:2013-05-02 10:40:55
【问题描述】:
我创建了JFrame,其中包含和InternalFrame,它绘制了移动的图形(每个图形都是另一个Thread)我想pauseButton让它暂停,所以我想在传递的对象上同步它们。
但是当我点击暂停按钮时,整个窗口都冻结了,我无法点击播放按钮 另一件事是当时只有一个在运行,我希望它们全部运行然后全部暂停。
class A extends JFrame{
....
Object o = new Object();
JButtton pauseButton = new JButton("pause");
JButtton playButton = new JButton("play");
B b = new B(o);
pauseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
synchronized (synchronizator) {
try {
synchronizator.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
playButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
synchronized (synchronizator) {
synchronizator.notifyAll();
}
}
...
}
class B extends JInternalFrame{
Object o;
B(Object o){this.o = o}
./...
many... C thread = new C(o);
....
}
class C extends Thread{
Object o;
booolean running;
public void run(){
while(running){
synchronized(o){
}
}
}
}
【问题讨论】:
-
which draws figures which are moving(each figure is another Thread)请问为什么,那些线程连接数据库,RMI,套接字,读取 FileIO ???,如果不是,那么只使用一个 Swing Timer 正如@Tom Hawtin 已经建议的那样 - tackline,吨代码这里的示例关于将所有(准备好的)对象放入数组并在paintComponent内部根据任何条件从数组中拾取对象
标签: java multithreading swing synchronization wait