callable与future的应用

public class CallableAndFuture {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ExecutorService service = Executors.newSingleThreadExecutor();
        Future<String> future = service.submit(new Callable<String>(){

			public String call() throws Exception {
				Thread.sleep(2000);
				return "hello world";
			}
        	
        });
        System.out.println("等待结果");
        try {//这个不须要Handler,能够直接拿到结果。
			System.out.println("拿到结果:="+future.get());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
相关文章
相关标签/搜索