相关文章推荐
眉毛粗的木瓜  ·  Shop, Catering & ...·  4 月前    · 
安静的领结  ·  Control.BeginInvoke ...·  1 年前    · 
光明磊落的卤蛋  ·  逃狱兄弟3_百度百科·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
    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)

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.

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());
                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.
– user6857832
                Sep 30, 2016 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, 2016 at 16:18
                I want each thread having a 5 iteration loop, so each thread would print 5 start and 5 end messages.
– user6857832
                Sep 30, 2016 at 16:31
                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.
– user6857832
                Sep 30, 2016 at 16:36
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.