我已经为记录用户 Activity 的程序编写了一个插件。如果用户按下按钮并因此触发操作,则应将一些内容写入数据库。此过程应并行运行,因为它不应延迟触发的操作:

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()方法,该方法似乎也可以工作。

你们有什么感想?

更新:
谢谢你们的反馈!

最佳答案

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

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

    }
}

关于java - Java多线程: positioning of methods,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16604847/

10-10 02:45