Created
August 15, 2011 18:48
-
-
Save clarenced/1147419 to your computer and use it in GitHub Desktop.
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
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