0

I am running a ThreadPoolExecutor with a number of Threads. I want to manipulate some data in the Threads, so I want to send an object to the thread and after it finishes use the data. The only way I know how is to wait until the thread finished with join() and then use the data. The problem is, that I can't get a single thread from the ThreadPoolExecutor. I would like to do something like this:

Thread t = ThreadPoolExecutor.getThread();
t.start();
t.join();

rest of code....

Does anyone have a way? I know that I can use Callable but I want to avoid this, as I already have pre-existing code...

Bimalesh Jha
  • 1,464
  • 9
  • 17
ron reish
  • 81
  • 1
  • 1
  • 5

1 Answers1

4

Yes you can submit, get a Future back and get() on the Future

Future<SomeResult> future = executorService.submit(new Callable<SomeResult>(){
    public SomeResult call(){
        //do work
        return new SomeResult();
    }
});

future.get();//suspends the current thread until the SomeResult callable returns.

Basically, the call() invocation is done on one of the Threads in the pool and the get() suspends the current thread similar to how t.join() suspends the current thread.

John Vint
  • 39,695
  • 7
  • 78
  • 108