I have a JButton to invoke my thread. But what I actually want to do is to stop the thread just after the one minute! My actionListener Method is:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
new Frame2().setVisible(true);
Thread t=new Thread(new Frame2());
t.start();
}
My thread to run for only one minute is as follow:
public void run(){
int i;
while(!Thread.currentThread().isInterrupted()){
for(i=0;i<=100;i++){
if(i==100){
Thread.currentThread().interrupt();
}
try {
Thread.currentThread().sleep(600);
} catch (InterruptedException ex) {
System.out.print("THREAD CLOSED");
return;
}
}
System.out.print("DOING THINGS BLA BLA");
}
}
The Problem: I have stopped the thread after one minute successfully, but I was not able to do anything desired in it. I just want to know that how can I achieve this in order to run the thread for only one minute and inside the thread I want to do my things! But how? Am I wrong with this approach? If, yes then what should be the right approach?