Skip to content

Instantly share code, notes, and snippets.

@clarenced
Created August 15, 2011 18:48
Show Gist options
  • Save clarenced/1147419 to your computer and use it in GitHub Desktop.
Save clarenced/1147419 to your computer and use it in GitHub Desktop.
package com.grosdim.randomCallable;
public class CallableExample {
static final int NB_THREADS = 5;
public static class StringCallable implements Callable<String>{
private String stringToUpper;
public StringCallable(String string){
this.stringToUpper = string;
}
@Override
public String call() throws Exception {
return this.stringToUpper.toUpperCase();
}
}
public static void main(String[] args) throws InterruptedException, ExecutionException{
List<Future<String>> futures = new ArrayList<Future<String>>();
ExecutorService executor = Executors.newFixedThreadPool(NB_THREADS);
String[] stringArray = {"java", "callable", "myblog", "hello world", "bonjour tout le monde", "What's going on", "etc"};
for( String s : stringArray){
Callable<String> callable = new StringCallable(s);
Future<String> future = executor.submit(callable);
futures.add(future);
}
for(Future<String> f : futures){
String result = f.get();
System.out.println(result);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment