作为我最后一年项目的一部分,我开发了一个桌面应用程序,如果存在的话,它属于“图形 IDE”类别。我已经实现了 Jessy James Garrett Visual Vocabulary for Information Architecture and Interaction Design 的一小部分,因此用户可以绘制表示用户在 web 应用程序中的体验的图表(换句话说,有向图),将 HTML 模板分配给页面,并将一些代码写入将在编译应用程序后执行的连接器/转换,并且用户点击相应的超链接。
(我不能发布超过一个超链接,但我指的是页面和连接器,例如 JJG 的视觉词汇中描述的用户体验元素)
因此,我正在使用不同的 SwingWorkers 生成一组 C++ 源文件,图表将转换成这些文件。查看日志,我发现我的应用程序总是在创建新线程,而不是重复使用它们。
15:29:55.750 [SwingWorker-pool-2-thread-26] DEBUG i.v.a.ui.worker.ConnectorGenerator - Building source code for transition: connector_29
15:29:55.750 [SwingWorker-pool-2-thread-26] DEBUG i.v.a.ui.worker.ConnectorGenerator - Project retrieved: sasdasdasd
15:29:55.750 [SwingWorker-pool-2-thread-26] INFO i.v.a.webapp.htcpp.CTransition - Generated C:\Workspaces\PFC\aedifico-ui\sasdasdasd\connector_29_action.h...
15:29:55.750 [SwingWorker-pool-2-thread-26] INFO i.v.a.webapp.htcpp.CTransition - Generated C:\Workspaces\PFC\aedifico-ui\sasdasdasd\connector_29_action.cpp...
15:29:55.750 [SwingWorker-pool-2-thread-26] DEBUG i.v.a.ui.worker.ConnectorGenerator - Transition generated at: C:\Workspaces\PFC\aedifico-ui\sasdasdasd/connector_29_action.cpp
我所有的 worker 都做同样的两件事:
生成一对 C++ 源文件和头文件,使用 Freemarker 模板引擎。
发送消息到
EDT
通过 发布过程机制 告知用户情况如何 去。
我相信我已经编码了 SwingWorker
小心点。我特别担心 FileWriter 实例没有按预期关闭,但我看不出 ThreadPoolExecutor
的原因没有重用它之前创建的线程。
奇迹发生在 ConnectorGenerator
. BaseWorker
延伸SwingWorker<V,T>
并仅持有与组件通信以向用户显示消息的行为。
public class ConnectorGenerator<Void> extends BaseWorker<Void> {
@Override
public Void doInBackground() {
Transition transition = connector.getModel().getTransition();
logger.debug("Building source code for transition: {}", transition.getName());
logger.debug("Project retrieved: {}", project.getName());
try {
publish("Generating transition (%s) source code at %s", transition.getName(), project.getBaseDir());
/**
* Transition.build(String), attached below, is responsible of generating and writing the files
*/
transition.build(project.getBaseDir().getPath());
publish("Transition generated.");
} catch (BuilderException e) {
logger.error("Error: {}", e);
publish("There was an error that prevented generating %s's source code", transition.getName());
}
logger.debug("Transition generated at: {}/{}", project.getBaseDir(), transition.getSource());
return null;
}
}
还有 Transition.build(String)
方法,包括丑陋的 try-catch-finally block :
@Override
public void build(String path) throws BuilderException {
generateFile(String.format("%s%s%s", path, File.separator, getHeader()), "action.h.ftl");
generateFile(String.format("%s%s%s", path, File.separator, getSource()), "action.cpp.ftl");
}
private void generateFile(String path, String templateName) throws BuilderException {
FileWriter out = null;
try {
Map<String, Object> model = new HashMap<String, Object>();
model.put("transition", this);
Configuration config = new Configuration();
config.setClassForTemplateLoading(CTransition.class, "/");
config.setObjectWrapper(ObjectWrapper.DEFAULT_WRAPPER);
out = new FileWriter(path);
freemarker.template.Template template = config.getTemplate(templateName);
template.process(model, out, ObjectWrapper.BEANS_WRAPPER);
stdout.info("Generated {}...", path);
} catch (IOException e) {
throw new BuilderException(e);
} catch (TemplateException e) {
throw new BuilderException(e);
} finally {
if (out != null)
try {
out.flush();
out.close();
} catch (IOException e) {
throw new BuilderException(e);
}
}
}
有人看到或知道我可能遗漏了什么吗?
最佳答案
关于java - 这个SwingWorker是不是没有复用ThreadPoolExecutor中的Threads?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3102517/