For the learning purpose I'm struggling to run a Thread WITHOUT using Timer,TimerTask,SwingWorker. Coming from PHP background EDT is something new & also hard for me to understand, I need your understanding for my situation.
I created/construct the following code, But I want to ask that is this Thread-safe? It makes me even more confuse because I ask this on another thread they just say that it's safe, but ugly . They just say the sentence, i would think that it isn't safe because that Thread.sleep() coming from the main thread, so why it's safe?
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//========================================================================
Thread worker = new Thread() {
public void run() {
// Simulate doing something useful.
for(int i=0; i<=10; i++) {
final int count = i;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
countLabel1.setText(Integer.toString(count));
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
} // end loop
SwingUtilities.invokeLater(new Runnable() {
public void run() {
statusLabel.setText("Completed.");
}
});
}
};
worker.start();
//==========================================================================
}
});