public class CallableExample {

  public static void main(String[] args) throws Exception {

    String text = "Ala ma kota";
    ExecutorService es = newFixedThreadPool(3);
    int result = 0;
    for (String word : text.split(" ")) {
      Future<Integer> fut = es.submit(callable(word));
      result += fut.get();

    out.println("Number of non-space chars: " + result);

  static Callable<Integer> callable(final String word) {
    return new Callable<Integer>() {

      public Integer call() throws Exception {
        out.println(threadName() + word);
        return word.length();

  static String threadName() {
    return Thread.currentThread().getName() + ",";

Number of non-space characters: 9

Note: you execute Runnable and submit Callable on the ExecutorService. The get method on Future is bloking operation, waiting for the result.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s