2013-04-14 41 views

回答

0

如果jar然後Callable接口正是做了這樣的:

class CalculateSomeString implements Callable<String>{ 
    @Override 
    public String call() throws Exception { 
     //Simulate some work that it takes to calculate the String 
     Thread.sleep(1000); 
     return "CoolString"; 
    } 
} 

並運行它

public static void main(String[] args) throws InterruptedException, ExecutionException { 
    ExecutorService service = Executors.newFixedThreadPool(1); 
    Future<String> future = service.submit(new CalculateSomeString()); 
    //this will block until the String has been computed 
    String result = future.get(); 
    service.shutdown(); 
    System.out.println(result); 
}