2013-05-17 130 views
1

我已经编写了一个记录用户活动的程序的插件。如果用户按下按钮并触发某个动作,则应将一些内容写入数据库。这个过程应该在并行运行,因为它不应该延迟触发动作:Java多线程:方法的定位

if (con != null) { 
     SmartClientKernel.addPooledTask(new Runnable() { 
      @Override 
      public void run() { 
       ActionDispatcher.getInstance().addDispatchExtension(new IDispatchExtension() { 
        @Override 
        public void preprocessActionExecution(RPAction action) { 
         Calendar cal = Calendar.getInstance(); 
         Date startTime = cal.getTime(); 
         String actionName = action.getName(); 
         java.sql.Timestamp sqlTime = new java.sql.Timestamp(startTime.getTime()); 
         UUID id = UUID.randomUUID(); 
         PreparedStatement statement; 
         try { 
          statement = con.prepareStatement("INSERT INTO " + tableName 
            + " (ID, USERNAME, SESSIONID, PROJECTNAME, STARTTIME, ACTIONTYPE, ACTIONNAME) VALUES (?,?,?,?,?,?,?)"); 
          statement.setString(1, id.toString()); 
          statement.setString(2, userName); 
          statement.setString(3, sessionId); 
          statement.setString(4, projectName); 
          statement.setTimestamp(5, sqlTime); 
          statement.setString(6, "Action"); 
          statement.setString(7, actionName); 
          statement.execute(); 
          statement.close(); 
         } catch (SQLException e) { 
          Log.getLogger().log(Level.SEVERE, e.getMessage(), e); 
         } 

        } 
       }); 
      } 
     }); 
    } 

事实上,它工作正常,但我想知道如果编程风格是确定的,那是因为我可以移动的块:

SmartClientKernel.addPooledTask(new Runnable() { 
     @Override 
     public void run() { 

进入方法preprocessActionExecution(),这似乎也工作。

你们认为什么?

更新: 谢谢你们的反馈!

+0

我建议到数据库的代码从线程中分离出来。将DB资料与其他功能方面分开以便于维护。 –

+0

“SmartClientKernel.addPooledTask”和“ActionDispatcher.getInstance()。addDispatchExtension”)的方法是什么 - 你有任何文档吗? –

回答

0

如果你不喜欢你这样做的方式,这是另一种方式:

anyClassMethod() { 
    if (con != null) { 
     SmartClientKernel.addPooledTask(new MyRunnable(con, tableName, userName, sessionId, projectName)); 
    } 
} 

private class MyRunnable implements Runnable { 

    Connection con; 
    String tableName, userName, sessionId, projectName; 

    MyRunnable(Connection con, String tableName, String userName, String sessionId, String projectName) { 
     this.con = con; 
     this.tableName = tableName; 
     this.userName = userName; 
     this.sessionId = sessionId; 
     this.projectName = projectName; 
    } 

    @Override 
    public void run() { 
     ActionDispatcher.getInstance().addDispatchExtension(new MyIDispatchExtension(con, tableName, userName, sessionId, projectName)); 
    } 
} 

private class MyIDispatchExtension (extends or implements) IDispatchExtension { 

    Connection con; 
    String tableName, userName, sessionId, projectName; 

    public MyIDispatchExtension(Connection con, String tableName, String userName, String sessionId, String projectName) { 
     this.con = con; 
     this.tableName = tableName; 
     this.userName = userName; 
     this.sessionId = sessionId; 
     this.projectName = projectName; 
    } 

    @Override 
    public void preprocessActionExecution(RPAction action) { 
     Calendar cal = Calendar.getInstance(); 
     Date startTime = cal.getTime(); 
     String actionName = action.getName(); 
     java.sql.Timestamp sqlTime = new java.sql.Timestamp(startTime.getTime()); 
     UUID id = UUID.randomUUID(); 
     PreparedStatement statement; 
     try { 
      statement = con.prepareStatement("INSERT INTO " + tableName 
        + " (ID, USERNAME, SESSIONID, PROJECTNAME, STARTTIME, ACTIONTYPE, ACTIONNAME) VALUES (?,?,?,?,?,?,?)"); 
      statement.setString(1, id.toString()); 
      statement.setString(2, userName); 
      statement.setString(3, sessionId); 
      statement.setString(4, projectName); 
      statement.setTimestamp(5, sqlTime); 
      statement.setString(6, "Action"); 
      statement.setString(7, actionName); 
      statement.execute(); 
      statement.close(); 
     } catch (SQLException e) { 
      Log.getLogger().log(Level.SEVERE, e.getMessage(), e); 
     } 

    } 
} 
3

我一直觉得,如果代码可能一节有自己的名字,那么它应该有自己的名称

或者,换一种说法,如果它看起来像一个类,就像一个类就应该

所以 - 回答你的问题 - 在我看来,每一个匿名内部类应该是独立的类。尽管他们不需要公开课,但他们会很好,但他们应该被分解出来并有一个名字。

此外 - 您准备的声明应该在别处准备。准备使用它是浪费时间。