There is no sense to spawn 1000 concurrent threads queryng an SQL database - just because typical database cannot bear so many simultaneous connections, typical number is 10-15. The solution is to organize a special thread pool, where each thread has an open connection and serves tasks which access database. Tasks are submitted to a common blocking queue, and working threads read from that queue in a loop. Tasks are instances of
interface DBRunnable {
public void run(Connection conn);
}
working threads pass their connection to the tasks:
public void run() {
Connection conn = DriverManager.getConnection(...);
while (true) {
DBRunnable task=queue.take();
task.run(conn);
}
}