You can use another thread (or runnable) to get query results, but make sure that the query-task is self-contained: it opens a connection (or gets one from the conneciton pool), fires the query, gets the results, stores the results in some variable (e.g. HashMap), closes the query, the result-set and the connection (or returns it to the pool). When all this is done (with appropriate try-catch-finally blocks), the task can then 'send' the data to the main processing thread.
Using the method described in the answer by TeresaCarrigan, following an example how a "worker thread" can 'send' data to the "main thread":
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class Q21176667 implements Runnable {
private static volatile boolean stop;
public static void main(String... args) {
Thread t = new Thread(new Q21176667());
t.start();
sleep(200);
stop = true;
}
private BlockingQueue<Long> q = new ArrayBlockingQueue<Long>(100);
@Override
public void run() {
try {
while (!stop) {
if (q.peek() == null) {
new Thread(new GetUpdates(this)).start();
}
long l = q.take();
System.out.println("Main sleep: " + l);
sleep(l);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void add(Collection<Long> moreData) {
for (Long l : moreData) {
q.add(l);
}
}
static class GetUpdates implements Runnable {
final Q21176667 processor;
public GetUpdates(Q21176667 processor) {
this.processor = processor;
}
@Override
public void run() {
List<Long> vlist = new ArrayList<Long>();
// Run query, process results
sleep(10);
Random r = new Random();
for (int i = 0; i < 3; i++) {
vlist.add(new Long(r.nextInt(10)));
}
System.out.println("Adding " + vlist.size() + " more sleeps.");
processor.add(vlist);
}
}
public static void sleep(long timeMs) {
try { Thread.sleep(timeMs); } catch (Exception ignored) {}
}
}
Output looks like:
Adding 3 more sleeps.
Main sleep: 5
Main sleep: 9
Main sleep: 0
Adding 3 more sleeps.
Main sleep: 3
Main sleep: 7
Main sleep: 8
etc.