I will us a contrived example to illustrate locking up the edt, then I will show how that example can be used off of the edt.
Disclaimer: I am going to do some ugly things, in the next version I will try to have removed most of this ugliness.
Lets just look at the action listener for now.
new ActionListener(){
public void actionPerformed(ActionEvent event){
label.setText(Thread.currentThread().getName());
}
}
I am going to change this to:
public void actionPerformed(ActionEvent event){
label.setText(Thread.currentThread().getName());
try{
Thread.sleep(500);
} catch(InterruptedException e){
//not to worry.
}
}
If you run this, when you click the button the program locks up for half a second and then it updates the label. So what we want to do is to get off of the EDT. To do that we just create a Runnable and start it in a new Thread.
public void actionPerformed(ActionEvent event){
Runnable r = new Runnable(){
public void run(){
label.setText(Thread.currentThread().getName());
try{
Thread.sleep(500);
} catch(InterruptedException e){
//not to worry.
}
}
};
new Thread(r).start();
}
}
Now when you click it there is no waiting, but the name changes because we are starting a new thread. This is not nescessarily a problem, but it is also not a good thing.
At this juncture we know why our gui locks up when we execute a long running task on the EDT and we know a way to avoid this lock up. Now we are going to look at refining these techniques remember one of the goals of this tutorial is to make the application essentially a single threaded application. Right now, it is using as many threads as we can click.