How to deal with the return value of the thread?

How to deal with the return value of the thread?
1, the main thread waits law drawback: the need to implement your own logic loop waiting when there are many variables that need to wait, abnormal code is bloated.
2, join thread class () blocking the current thread to wait for the child thread processed. Cons: not enough fine control.
3, to get through FutureTask Or thread pool callable interface through. 

First, then, directly on the bar code, the first method we first began. Create a class CycleWait, as follows:
class CycleWait the implements the Runnable {public 

  Private String value; 

  @Override 
  public void RUN () { 
    the try { 
      Thread.currentThread () SLEEP (5000);. 
    } the catch (InterruptedException E) { 
      e.printStackTrace (); 
    } 
    value = "WE have have now DATE "; 
  } 

  public static void main (String [] args) {throws InterruptedException 
    CycleWait = new new CycleWait the wait (); 
    the Thread the Thread Thread new new = (the wait); 
    Thread.start (); 
    // null value when the time has been loop until there is time value will be returned. 
    // less this step, you may remove the null value. 
    the while (== wait.value null) { 
      the try {  
        Thread.currentThread () SLEEP (100).;
      } the catch (InterruptedException E) {
        e.printStackTrace();
      }
    }
    System.out.println(wait.value);
  }
}

  When loop to CycleWait execution is complete, the output we have date now. 

Second, the loop is removed using the join method. A return to the same result.

class CycleWait the implements the Runnable {public 

  Private String value; 

  @Override 
  public void RUN () { 
    the try { 
      Thread.currentThread () SLEEP (5000);. 
    } the catch (InterruptedException E) { 
      e.printStackTrace (); 
    } 
    value = "WE have have now DATE "; 
  } 

  public static void main (String [] args) {throws InterruptedException 
    CycleWait = new new CycleWait the wait (); 
    the Thread the Thread Thread new new = (the wait); 
    Thread.start (); 
    // null value when the time has been loop until there is time value will be returned. 
    // less this step, you may remove the null value. 
    Thread.join (); 
    System.out.println (wait.value); 
  } 
}

  Third, the use FutureTask get results, be controlled.

public class myCallable implements Callable {
  @Override
  public String call() throws Exception {
    String value = "test";
    System.out.println("ready to work");
    Thread.currentThread().sleep(5000);
    System.out.println("task down");
    return value;
  }
}

  

public class FutureTaskDemo {

  public static void main(String[] args) throws ExecutionException, InterruptedException {
    FutureTask<String> futureTask = new FutureTask<String>(new myCallable());
    new Thread(futureTask).start();
    if (!futureTask.isDone()) {
      System.out.println("task has not ");
    }
    System.out.println("task reture:{}" + futureTask.get());
  }
}

  Fourth, the thread pool approach. Benefits: Thread submit multiple myCallable method can be achieved, is the thread pool to handle concurrent results.

public class ThreadPoolDemo {

  public static void main(String[] args) {
    //创建线程池
    ExecutorService executorService = Executors.newCachedThreadPool();
    //提交myCallable的任务去执行
    Future<String> future = executorService.submit(new myCallable());
    if (!future.isDone()) {
      System.out.println("task has not ");
    }
    try {
      System.out.println("task reture:{}" + future.get());
    } catch (InterruptedException e) {
      e.printStackTrace();
    } catch (ExecutionException e) {
      e.printStackTrace();
    } finally {
      //关闭线程池
      executorService.shutdown();
    }
  }

  

 

Guess you like

Origin www.cnblogs.com/shenwen/p/11277608.html