Langali Langali - 2 years ago 85
Java Question

Return values from Java Threads

I have a Java Thread like the following:

public class MyThread extends Thread {
MyService service;
String id;
public MyThread(String id) { = node;
public void run() {
User user = service.getUser(id)

I have about 300 ids, and every couple of seconds - I fire up threads to make a call for each of the id. Eg.

for(String id: ids) {
MyThread thread = new MyThread(id);

Now, I would like to collect the results from each threads, and do a batch insert to the database, instead of making 300 database inserts every 2 seconds.

Any idea how I can accomplish this?

Answer Source

If you want to collect all of the results before doing the database update, you can use the invokeAll method. This takes care of the bookkeeping that would be required if you submit tasks one at a time, like daveb suggests.

private static final ExecutorService workers = Executors.newCachedThreadPool();


Collection<Callable<User>> tasks = new ArrayList<Callable<User>>();
for (final String id : ids) {
  tasks.add(new Callable<User>()

    public User call()
      throws Exception
      return svc.getUser(id);

/* invokeAll blocks until all service requests complete, 
 * or a max of 10 seconds. */
List<Future<User>> results = workers.invokeAll(tasks, 10, TimeUnit.SECONDS);
for (Future<User> f : results) {
  User user = f.get();
  /* Add user to batch update. */
/* Commit batch. */
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download