The pool.offer(id)
means you put a used id back in the queue - so it can be reused later on by another thread. That is probably going to be an issue (since the queue is FIFO you will get a duplicate ID on the 100,001st insertion).
In any case, it seems very complicated when a static AtomicInteger
would do the same thing without having to use a queue:
class ThreadTask implements Runnable {
private final AtomicInteger id;
ThreadTask(AtomicInteger id) {
this.id = id; //in your main thread: id = new AtomicInteger(minId);
}
@Override
public void run() {
dbConnection = getDBConnection();
preparedStatement = dbConnection.prepareStatement(INSERT_SQL);
preparedStatement.setString(1, String.valueOf(id.getAndIncrement()));
preparedStatement.setString(2, ACCOUNT);
preparedStatement.executeUpdate();
}
}
Note: as commented, your database can probably assign unique IDs for you.