/**
*
*/
package de.tutorials;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
/**
* @author Thomas.Darimont
*
*/
public class InterruptedThreadExample extends JFrame {
JLabel label;
Runnable runnable = new Runnable() {
@Override
public void run() {
while(true){
label.setText("Time: " + System.currentTimeMillis());
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
System.out.println("interrupted");
//handle interrupt....
}
}
}
};
Thread thread;
public InterruptedThreadExample(){
super("InterruptedThreadExample");
setDefaultCloseOperation(EXIT_ON_CLOSE);
thread = new Thread(runnable);
label = new JLabel("Time: 0");
JButton btn = new JButton("interrupt");
btn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
thread.interrupt();
}
});
add(btn,BorderLayout.SOUTH);
add(label,BorderLayout.CENTER);
pack();
setVisible(true);
thread.start();
}
/**
* @param args
*/
public static void main(String[] args) {
new InterruptedThreadExample();
}
}