我想使用 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中还是可以成功保存数据但是不稳定
繁华开满天机
相关分类