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();
		}
	}

}
相關文章
相關標籤/搜索