I am facing a problem in Thread .Here is my code
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");
}
}
CustomBrightActionPerformed
is a method of another class that deals with Action Event and Mymos
is inner class .The problem is that I am sleeping creed Thread
but my main thread is also sleeping for 3 seconds.Why ?
you are invoking sleep()
on main thread,
sleep()
is static
method you are just using its Thread's instance (creed) to call it which is discouraged, It sleeps the Thread
in which this method is being called
if you want to pause creed thread then you need to execute Thread.sleep()
from its run()
method