我相对于多线程还比较新,想要使用Swingworker线程执行后台任务 - 调用的方法实际上并不返回任何内容,但我希望在完成时得到通知。
从我看过的所有关于使用SwingWorkers的教程中,它们都是这样创建的:
new SwingWorker<String, Void>;
如果没有数据返回,将我的声明为Void是否合适?
到目前为止,我所拥有的代码似乎无法正常工作:
private void crawl(ActionEvent evt)
{
try
{
SwingWorker<Void, Void> crawler = new SwingWorker<Void, Void>()
{
@Override
protected Void doInBackground() throws Exception
{
Discoverer discover = new Discoverer();
discover.crawl();
return null;
}
@Override
protected void done()
{
JOptionPane.showMessageDialog(jfThis, "Finished Crawling", "Success", JOptionPane.INFORMATION_MESSAGE);
}
};
crawler.execute();
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(this, ex.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
}
}
任何反馈/建议都将不胜感激,因为多线程是我编程中薄弱的领域。