2013-05-17 47 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

我一直覺得,如果代碼可能一節有自己的名字,那麼它應該有自己的名稱

或者,換一種說法,如果它看起來像一個類,就像一個類就應該

所以 - 回答你的問題 - 在我看來,每一個匿名內部類應該是獨立的類。儘管他們不需要公開課,但他們會很好,但他們應該被分解出來並有一個名字。

此外 - 您準備的聲明應該在別處準備。準備使用它是浪費時間。