Created
January 19, 2017 07:07
-
-
Save DinoZhang/27e38b21c5a00052f1d0c293ccb97552 to your computer and use it in GitHub Desktop.
test Executors
This file contains 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
@SuppressWarnings("unchecked") | |
public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException { | |
final ThreadLocal<String> test = new ThreadLocal<>(); | |
int i =1; | |
//ExecutorService executorService = Executors.newSingleThreadExecutor(); | |
// ExecutorService executorService = Executors.newCachedThreadPool(); | |
//ExecutorService executorService = Executors.newFixedThreadPool(10); | |
ExecutorService executorService = Executors.newScheduledThreadPool(10); | |
// while (i <= 100) { | |
// Future future = executorService.submit(new Runnable() { | |
// @Override | |
// public void run() { | |
// test.set(Thread.currentThread().getName()); | |
// | |
// System.out.println(test.get()); | |
// } | |
// }); | |
// if() | |
// i++; | |
// } | |
while (i <= 10) { | |
Future<String> future = executorService.submit(new Callable() { | |
@Override | |
public Object call() throws Exception { | |
test.set(Thread.currentThread().getName()); | |
System.out.println(test.get()); | |
Thread.sleep(2000); | |
return test.get(); | |
} | |
}); | |
if(!future.isDone()){ | |
System.out.println("future is not done:"+i+":"+future.get()); | |
} | |
System.out.println("future:"+future.get()); | |
System.out.println("future:"+future.get(1000, TimeUnit.MILLISECONDS)); | |
i++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment