2010-06-23 33 views
2

作爲我最後一年項目的一部分,我開發了一個桌面應用程序,如果存在這種情況,它就適合「圖形化IDE」類別。我已經實現了Jessy James Garrett Visual Vocabulary for Information Architecture and Interaction Design的一小部分,因此用戶可以繪製代表用戶在webapp中的體驗的圖表(換言之,有向圖),將HTML模板分配給頁面,並將一些代碼寫入連接器/轉換中一旦應用程序被編譯並且用戶點擊相應的超鏈接,就執行該程序。這個SwingWorker不能重用ThreadPoolExecutor中的線程嗎?

(我不能發佈多個超鏈接,但我指的是網頁和連接器的用戶體驗在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 

我所有的工人做同樣的兩件事情:

  1. 生成一對下使用 Freemarker模板引擎++源文件和頭文件。

  2. 通過 發佈流程機制將消息發送到EDT到 告知用戶事情是如何去 。

我相信我已經仔細編碼了SwingWorker s。我特別擔心FileWriter實例沒有按預期關閉,但我看不到ThreadPoolExecutor不重複使用它之前創建的線程的原因。

魔術發生在ConnectorGeneratorBaseWorker擴展爲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塊:

@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); 
      } 
}  
} 

有誰看到或知道的東西我可能丟失?

+0

我沒有在你的代碼中調用的ThreadPoolExecutor看到。 – 2010-06-23 14:37:38

+0

@Romain,當您調度worker的執行時它會被調用:ConnectorGenerator worker = new ConnectorGenerator(); worker.execute(); – 2010-06-23 14:41:55

+0

我沒有提到,SwingWorkers使用10個線程的ThreadPoolExecutor,如果10個正在運行任務,它只會創建更多的線程。 – 2010-06-23 14:44:10

回答

相關問題