ScheduledExecutorService仅循环一次 [英] ScheduledExecutorService only loops once

查看:106
本文介绍了ScheduledExecutorService仅循环一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个每秒循环的ScheduledExecutorService线程,但是到目前为止,它仅循环一次.

I am trying to implement a ScheduledExecutorService thread that loops every second, but as of right now it only loops once.

我的问题是如何设置它,使其周期性地循环而不是一次迭代?

My question is how do I set it up so that it loops periodically instead of one iteration?

此外,如何将连接池传递到线程中,以便每次迭代都可以查询数据库?非常感谢您的帮助.

Also,how do I pass the connection pool into the thread so that I can query a database every iteration? Any help is much appreciated.

public static void main(String[] args) throws InterruptedException {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    AdminManager frame = new AdminManager();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });

        BoneCP connectionPool = null;
        Connection connection = null;

        try {
            // load the database driver (make sure this is in your classpath!)
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        try {
            // setup the connection pool
            BoneCPConfig config = new BoneCPConfig();
            config.setJdbcUrl("jdbc:mysql://192.0.0.1:3306/database"); // jdbc url specific to your database, eg jdbc:mysql://127.0.0.1/yourdb
            config.setUsername("root"); 
            config.setPassword("");
            connectionPool = new BoneCP(config); // setup the connection pool

            connection = connectionPool.getConnection(); // fetch a connection

            if (connection != null){
                System.out.println("Connection successful!");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
        exec.schedule(new Runnable(){
            @Override
            public void run(){
                System.out.println("Working ... ");

            }
        }, 1, TimeUnit.SECONDS);

        //connectionPool.shutdown(); // shutdown connection pool.
}

推荐答案

有一个scheduleAtFixedRate方法.要将某些内容传递给匿名类,需要将其声明为final.而且它必须在相同的范围内.

there's a scheduleAtFixedRate method. To pass something in to a anonymous class it needs to be declared final. And it needs to be in the same scope.

现在您拥有的代码正在关闭连接,如果您打算将其传递给另一个线程,则需要保持连接打开.

Also the code you have now is closing the connection, if you intend on passing it to another thread you need to keep it open.

!编辑一些示例代码

public class Whatever {
    public static void main(String[] args) throws Exception {
        // ... do your frame thing

        loadDataBaseDriver();
        BoneCP connectionPool = createConnectionPool();

        try {
            final Connection connection = connectionPool.getConnection();
            ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();

            exec.scheduleAtFixedRate(new Runnable(){
                @Override
                public void run(){
                    System.out.println("Working ... ");

                    // use connection
                }
            }, 0, 1, TimeUnit.SECONDS);
        } catch (SQLException e) {
          // do whatever
        }
    }

    public static BoneCP createConnectionPool() throws SQLException {
        BoneCPConfig config = new BoneCPConfig();
        config.setJdbcUrl("jdbc:mysql://192.0.0.1:3306/database"); // jdbc url specific to your database, eg jdbc:mysql://127.0.0.1/yourdb
        config.setUsername("root"); 
        config.setPassword("");
        connectionPool = new BoneCP(config);
        return connectionPool;
    }

    public static void loadDataBaseDriver() {
        try {
            // load the database driver (make sure this is in your classpath!)
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
    }

}

我不知道您正在调用的方法的签名,因此错误可能是错的

I don't know the signatures of the methods you are calling so the errors might be wrong

这篇关于ScheduledExecutorService仅循环一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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