该SwingWorker是否不在ThreadPoolExecutor中重用线程? [英] Is this SwingWorker not reusing Threads in ThreadPoolExecutor?

查看:89
本文介绍了该SwingWorker是否不在ThreadPoolExecutor中重用线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为我的最后一年"项目的一部分,我开发了一个桌面应用程序,如果存在的话,它适合图形IDE"的类别.我已经实现了 Jessy James Garrett信息体系结构和交互设计的视觉词汇的一小部分. ,因此用户可以绘制一个表示用户在Web应用程序中的体验的图(即一个有向图),将HTML模板分配给页面,并将一些代码写入到将在应用程序被编译且用户单击相应的超链接.

As part of my Final Year Project I've developed a desktop application, which fits in the category of the "graphical IDE" if such a thing exists. I've implemented a little subset of Jessy James Garrett Visual Vocabulary for Information Architecture and Interaction Design, so the user can draw a diagram (a directed graph in other words) representing the user's experience in a webapp, assign HTML templates to pages and write some code to a connector/transition that will be executed, once the application is compiled and the user clicks the corresponding hyperlink.

(我不能发布多个超链接,但我指的是页面和连接器,例如JJG的Visual Vocabulary中描述的用户体验元素)

(I can't post more than one hyperlink, but I'm referring to pages and connectors like the Elements of User Experience described in JJG's Visual Vocabulary)

因此,我正在使用不同的SwingWorkers生成一组C ++源文件,该图将这些源文件转换为该文件.查看日志,我发现我的应用程序始终在创建新线程,而不是重复使用它们.

So I'm using a different SwingWorkers to generate a set of C++ source files in which the diagram gets translated into. Looking at the logs, I see that my app is always creating new threads, instead of reusing them.

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

我所有的工人都做同样的两件事:

All my workers do the same two things:

  1. 使用以下命令生成一对C ++源文件和头文件 Freemarker模板引擎.

  1. Generate a pair of C++ source and header files using Freemarker template engine.

通过以下方式将消息发送到EDT 发布过程机制 告知用户情况如何 去.

Send messages to the EDT through the publish-process mechanism to inform the user how things are going.

我相信我已经对SwingWorker进行了仔细的编码.我特别担心FileWriter实例未按预期关闭,但是我看不到ThreadPoolExecutor不重用以前创建的线程的原因.

I believe I've coded the SwingWorkers carefully. I was especially worried about the FileWriter instances not being closed as expected, but I can't see the reason the ThreadPoolExecutor is not reusing the threads it previously created.

魔术发生在ConnectorGenerator中. BaseWorker扩展了SwingWorker<V,T>,仅保留了与组件通信以向用户显示消息的行为.

The magic happens in the ConnectorGenerator. The BaseWorker extends SwingWorker<V,T> and just holds the behaviour to communicate to a component to display the messages to the user.

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块:

And the Transition.build(String) method, including an ugly-as-hell 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);
            }
}       
}

有人看到或知道我可能会丢失的东西吗?

Does anyone see or knows something I'm probably missing?

推荐答案

也许这对您很有趣: SwingWorker死锁,原因是swingworker池中有一个线程

这篇关于该SwingWorker是否不在ThreadPoolExecutor中重用线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆