Skip to content

Instantly share code, notes, and snippets.

@DinoZhang
Created January 19, 2017 07:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DinoZhang/27e38b21c5a00052f1d0c293ccb97552 to your computer and use it in GitHub Desktop.
Save DinoZhang/27e38b21c5a00052f1d0c293ccb97552 to your computer and use it in GitHub Desktop.
test Executors
@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