0
    public class Threadz implements Callable<String>{
    private boolean flag = false;

    @Override
    public String call() throws Exception {
        // TODO Auto-generated method stub
        for(int i = 0; i < 3; i++){
            Thread.sleep(2000);
        }
        flag = true;
        return "Done";
    }

    public boolean isDone(){
        return flag;
    }
}

    public class MyMain {
    public static void main(String[] args) {
        ExecutorService exe = Executors.newSingleThreadExecutor();
        Threadz threadz = new Threadz();
        Callable<String> cthread = threadz;
        Future<String> fut = exe.submit(cthread);

        while(true){
            try {
                if(!threadz.isDone())
                    System.out.println("Do something while waiting...");
                else{
                    System.out.println(fut.get());
                    System.out.print("Do something with the result...");
                    break;
                }
                Thread.sleep(1000);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

My goal with the above code is to spawn a thread that will return a result but the calling thread should not be paused while waiting for the result. This actually works but am I doing this correctly? Is there a proper way of doing this? And also, is the callable thread automatically dies (frees resources and memory) after returning the result?

Thanks.

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
tambalolo
  • 1,035
  • 3
  • 14
  • 30
  • It's probably not a good idea to just sleep in a loop until the task is complete... I would personally say it's a bad idea to wait and do other work in one thread -- instead, block one thread while waiting for completion (i.e. `fut.get`), and once it's done, wake the other busier thread up. PS you should use [`Future.isDone`](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Future.html#isDone()) rather than roll your own in `Threadz`. – obataku Aug 28 '12 at 06:32
  • Thanks for your comment, veer. How about my last question? Is it just like any regular thread which soon dies after completion of its task? – tambalolo Aug 28 '12 at 06:40
  • no, the executor service's underlying worker thread will continue to run, waiting to consume tasks as they are submit for execution. You can, however, shut the service down via `ExecutorService.shutdown`. Once your task completes, the thread will then stop, too. – obataku Aug 28 '12 at 06:44

1 Answers1

1
String callbackReturn = threadz.get();

It waits if necessary for the computation to complete, and then retrieves its result.

        ExecutorService exe = Executors.newSingleThreadExecutor();
        Threadz threadz = new Threadz();
        Callable<String> cthread = threadz;
        Future<String> fut = exe.submit(cthread);
        String callbackReturn = threadz.get();

Current thread will wait here. if you dont want to pause it, then handle it result in other thread.

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103