栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

ScheduledExecutorService仅循环一次

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ScheduledExecutorService仅循环一次

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

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/610162.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号