-1

Here's my following code:

    ExecutorService executor = Executors.newFixedThreadPool(5);

    executor.submit(new Runnable() {
        public void run() {
            for (int i = 0; i < 5; i++) {
              System.out.println("Start"+"  "+Thread.currentThread().getName());
              try {
                Thread.sleep(100);
              } catch (InterruptedException e) {
                e.printStackTrace();
              }
              System.out.println("End!"+"  "+Thread.currentThread().getName());
            }
        }
    });

    executor.shutdown();
    executor.awaitTermination(1, TimeUnit.SECONDS);

Here's my output:

    Start  pool-1-thread-1
    End!  pool-1-thread-1
    Start  pool-1-thread-1
    End!  pool-1-thread-1
    Start  pool-1-thread-1
    End!  pool-1-thread-1
    Start  pool-1-thread-1
    End!  pool-1-thread-1
    Start  pool-1-thread-1
    End!  pool-1-thread-1

My understanding is that in my code there're 5 threads in FixedThreadPool. So when I run my code, it's supposed to output all 5 threads, right? Or Am I misunderstanding something?

In my output thread-1 starts and end everytime but isn't it supposed to output all 5 threads when it loops in for loop? Cause if only 1thread is doing the task then why do we even need 5 threads? Is there any way all 5 threads can output on the console?(I'm a newbie)

f1sh
  • 11,489
  • 3
  • 25
  • 51

1 Answers1

2

You are posting only one Runnable so your ExecutorService runs one task. It will use only one thread. To use multiple threads you must call submit multiple times or you can call invokeAll with a Collection of runnables.

EDIT

Something like this:

public void test() {
    int numberOfThreads = 5;
    ExecutorService executorService = Executors.newFixedThreadPool(numberOfThreads);
    for(int i=0; i<numberOfThreads; i++){
        executorService.submit(createRunnable());
    }
}

private Runnable createRunnable() {
    return new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < 5; i++) {
                System.out.println("Start" + "  " + Thread.currentThread().getName());
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("End!" + "  " + Thread.currentThread().getName());
            }
        }
    };
}
abbath
  • 2,444
  • 4
  • 28
  • 40
  • Thanks, i got it. But can you please provide me some code for `invokeAll`. I don't know how to do it with `invokeAll`. It will be appreciated. –  Sep 30 '16 at 14:50
  • What do you exactly want? 5 threads, each of them printing out a start and an end message? Or 5 thread each having a 5 iteration loop, so each thread would print 5 start and 5 end messages? – abbath Sep 30 '16 at 16:18
  • I want each thread having a 5 iteration loop, so each thread would print 5 start and 5 end messages. –  Sep 30 '16 at 16:31
  • Okay, I've added an example – abbath Sep 30 '16 at 16:33
  • But you said we can even do it with `invokeAll`. So how I can do with `invokeAll`.I don't want the whole code. I understand that `execute.invokeAll()` is all I've to do but what should I write inside `invokeAll()` parameters. –  Sep 30 '16 at 16:36
  • Ah yes it needs `Callable` instead of `Runnable`. – abbath Sep 30 '16 at 16:37
  • Can't we use `invokeAll` with `Runnable`? –  Sep 30 '16 at 16:39
  • Unfortunately not. However Callables are like runnables, just having a return value which can be used later on in `Future` objects, which can be useful. – abbath Sep 30 '16 at 16:48
  • Have you figured it out how to use `Callable`-s? I cam give an example about it in chat – abbath Sep 30 '16 at 16:49
  • Yes, I've used `Callable`-s before. –  Sep 30 '16 at 16:50
  • You mean I should move this conversation into chat? –  Sep 30 '16 at 16:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/124664/discussion-between-hemlata-and-abbath). –  Sep 30 '16 at 16:52
  • @Hemalata - Refer to this question for invokeAll usage: http://stackoverflow.com/questions/4691533/java-wait-for-thread-to-finish/39476747#39476747 – Ravindra babu Oct 02 '16 at 18:05