Currently, am using the method below for a basic swing app that I expect will grow in complexity
public void actionPerformed(ActionEvent e)
{
new Thread(new Runnable()
{
//do heavy db stuff--------------------------------------------
DAO dao = DAO.getInstance();
List<Employees> employeeList = dao.getAllEmployees();
EmployeeModel empModel = new EmployeeModel(employeeList);
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
//update swing GUI----------------------------------------
jTableEmployees.setModel(empModel);
}
});
}).start();
}
I have read this answer and answer and preparing early for the app will grow in complexity. My strategy to UPDATE THE UI of a large complex swing app is using Executor service as shown below.
import java.awt.event.ActionEvent;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.swing.SwingWorker;
public class SwiftExcecutor {
private final ExecutorService exec =
Executors.newFixedThreadPool(2);
private class GetEmployeesThread extends SwingWorker<String, String>{
@Override
protected String doInBackground() throws Exception {
return "complete.";
}
@Override
protected void done() {
//Safely update swing components------------------------------
}
}
private class GetContractorsThread extends SwingWorker<String, String>{
@Override
protected String doInBackground() throws Exception {
return "complete.";
}
@Override
protected void done() {
//Safely update swing components------------------------------
}
}
public void actionPerformed(ActionEvent e) {
GetEmployeesThread getAllEmployees = new GetEmployeesThread();
exec.execute(getAllEmployees);
GetContractorsThread getAllContractors = new GetContractorsThread();
exec.execute(getAllContractors);
}
}
My main concern is:
- Is using a dedicated threadpool to update a complex Swing app a sound strategy?
- Is the skeleton code with threadpool thread-safe? Can I update a component inside done(); method? As shown is skeleton code above?
- Should I expect any performance gains in UI response?(Not in sql queries and long running background task, I understand this is a different issues all together).