线程池例子

public class TestThread {
public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService service = Executors.newSingleThreadExecutor();
Future<String> future = service.submit(new Call());
String result = future.get(); //在此等待执行结果,如果没有这一句,就不等待,立即向下执行
System.out.println(result);
System.out.println("-----finish----------");
service.shutdown();
}
}
class Call implements java.util.concurrent.Callable<String>{
@Override
public String call() throws Exception {
TimeUnit.SECONDS.sleep(5);
return "I am running.";
}
}

你可能感兴趣的:(线程池例子)