Thread Future Mode

Multithreading Whether extends Thread or implements Runnable are to override the run method to achieve this in two ways child thread does not return a value.

If you want to achieve the main line Cheng Duizai monitor thread, so that the child thread returns a value, you can use implememts Callable <T> method:

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class App6 {
    public static void main(String[] args) throws Exception {
        ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
        Future<String> submit = newCachedThreadPool.submit(new TaskCallable());
        Result String = submit.get (); // Get Return value
        System.out.println(result);
    }

}

class TaskCallable implements Callable<String>{
    @Override
    public String call() throws Exception {
        System.out.println(Thread.currentThread().getName());
        return "Hello World";
    }
}

 

Guess you like

Origin www.cnblogs.com/pickKnow/p/11078908.html