Fre*_*911 2 java swing multithreading thread-sleep
我在Thread中遇到问题.这是我的代码
private void CustomBrightActionPerformed(java.awt.event.ActionEvent evt) {
creed = new Thread() {
public void run() {
final JFrame jfmae = new JFrame("Set BrightNess");
JLabel jla = new JLabel("!@#$");
jfmae.add(jla, BorderLayout.CENTER);
jfmae.setSize(180, 180);
jfmae.addMouseListener(new Mymos());
jfmae.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
jfmae.dispose();
}
});
jfmae.setVisible(true);
}
};
creed.start();
}
class Mymos extends MouseAdapter {
public void mousePressed(java.awt.event.MouseEvent evt) {
try {
creed.sleep(3000);
} catch (InterruptedException e) {
}
Bigh2MousePressed(evt);
}
private void Bigh2MousePressed(java.awt.event.MouseEvent evt) {
System.out.println("dsds");
}
}
Run Code Online (Sandbox Code Playgroud)
CustomBrightActionPerformed
是一个处理动作事件并且Mymos是内部类的另一个类的方法.问题是我正在睡觉creed Thread但我的主线程也在睡3秒钟.为什么?
你sleep()在主线程上调用,
sleep()是static你只是使用它的Thread的实例(信条)调用它的方法,这是不鼓励的,它睡眠Thread调用这个方法的方法
如果你想暂停信条线程,那么你需要Thread.sleep()从它的run()方法执行