gpt4 book ai didi

java - ScheduledExecutorService 只循环一次

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:56:48 24 4
gpt4 key购买 nike

我正在尝试实现每秒循环一次的 ScheduledExecutorService 线程,但截至目前它只循环一次。

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

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

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.
}

最佳答案

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ScheduledExecutorService.html

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

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

!编辑一些示例代码

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

}

我不知道你正在调用的方法的签名,所以错误可能错了

关于java - ScheduledExecutorService 只循环一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11379374/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com