ScheduledExecutorService只循环一次
问题描述:
我想实现一个ScheduledExecutorService线程循环每秒,但现在它只有一次循环。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;
}
}
}
我不知道你在呼唤这样方法的签名中的错误可能 是错误的
好。感谢您向我展示scheduleAtFixed Rate方法。我试图声明连接最后,它给了我以下错误...线程“主”java.lang.Error中的异常:未解决的编译问题: \t无法分配最终的局部变量连接。它必须是空白的并且不使用复合赋值 \t未处理的异常类型SQLException \t未处理的异常类型SQLException – scriptdiddy 2012-07-07 23:32:49
为什么?为什么在线程外部获得Connection?只需在线程中获取它,一个不错的本地限制变量(连接不是线程安全的)。使ConnectionPool成为final,然后使用它获取线程内部的连接。 – MJB 2012-07-08 07:32:21
yea可能分配得更好。 – megakorre 2012-07-08 08:27:24