我想使用 Executors 来加快将可用数据列表导入数据库的速度 public static void main(String[] args) { methodOpenDatabaseConnection(); ExecutorService newFixedThreadPool = Executors.newScheduledThreadPool(100); for (long i = 0; i < 1000; i++) { newFixedThreadPool.execute(new Runnable() { @Override public void run( {methodSaveDataToDatabaseMysql(); } }); } newFixedThreadPool.shutdown(); newFixedThreadPool.shutdown(); newFixedThreadPool.awaitTermination(60, TimeUnit.SECONDS); if (newFixedThreadPool.isShutdown()) { methodCloseConnectionDatabase(); } } public void methodSaveDataToDatabaseMysql() { try { preparedStatement = connection.prepareStatement("INSERT INTO `demo`" + " (`name`,`name2`,`name3`) VALUES (?,?,?)"); preparedStatement.setString(1, "name1"); preparedStatement.setString(2, "name2"); preparedStatement.setString(3, "name3"); preparedStatement.executeUpdate(); } catch (SQLException ex) { Logger.getLogger(WebsiteResourcesImpl1.class.getName()).log(Level.SEVERE, null, ex); } }我觉得save语句没有问题,因为在ExecutorService中还是可以成功保存数据但是不稳定
1 回答
繁华开满天机
TA贡献1816条经验 获得超4个赞
基于此代码,preparedStatement = connection.prepareStatement...
则preparedStatement
不是线程安全的。
考虑使其成为methodSaveDataToDatabaseMysql
方法的本地
添加回答
举报
0/150
提交
取消