Created
March 2, 2013 11:59
-
-
Save wfwei/5070689 to your computer and use it in GitHub Desktop.
Callable & Future
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.concurrent.Callable; | |
| import java.util.concurrent.ExecutionException; | |
| import java.util.concurrent.ExecutorService; | |
| import java.util.concurrent.Executors; | |
| import java.util.concurrent.Future; | |
| public class CallableAndFuture { | |
| /** | |
| * @param args | |
| */ | |
| public static void main(String[] args) { | |
| ExecutorService threadPool = Executors.newSingleThreadExecutor(); | |
| Future<String> future = threadPool.submit(new Callable<String>(){ | |
| @Override | |
| public String call() throws Exception { | |
| Thread.sleep(3000); | |
| return "future"; | |
| } | |
| }); | |
| try { | |
| System.out.println("waiting..."); | |
| System.out.println(future.get()); | |
| } catch (InterruptedException e) { | |
| e.printStackTrace(); | |
| } catch (ExecutionException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment