Restart java thread

I was looking for the reason that ExecutorService can execute the same thread multiple times. Because the normal thread life cycle ends on TERMINATED afaik ..

So,

public class TestThread extends Thread {

  AtomicInteger counter = new AtomicInteger(0);
  @Override
  public void run() {
    System.out.printf("%d\n", counter.addAndGet(1));
  }

  public static void main(String[] args) throws InterruptedException {
    ExecutorService es = Executors.newCachedThreadPool();
    TestThread t = new TestThread();

    es.execute(t);
    es.execute(t);

    es.shutdown();
  }
}

this works where I would really expect an illegal state exception, as in this case:

t.start();
t.start(); =>BAM!

Help really appreciate in order to solve the magic of the execution!

+4
source share
4 answers

ExecutorService never restarts Thread. A thread can never be restarted.

ExecutorService is a thread pool. It manages a set of long threads, and each thread is capable of performing many tasks.

es.submit(r) ExeuctorService, es Runnable, r; submit runnable .

, ExecutorService, run(), :

public void run() {
    while (true) {
        Runnable task = queue.take();
        task.run();
    }
}

, , - - shutdown(), . , .

+2

ExecutorService.execute(Runnable) Thread. ,

@Override
public void run() {
    System.out.printf("%d %d%n", counter.addAndGet(1),
        Thread.currentThread().getId());
}

(-). ,

1 10
2 11
+5

start() Thread , : Runnable . Thread execute(), run().

+2

, , ;)

:

package com.stackoverflow.test;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

public class TestOfCachedThreadPool {
  private static final int AMOUNT = 1000000;

  private static final class TestThread implements Runnable {
    private Map<Long, Long> mapOfUsage;

    private TestThread(Map<Long, Long> mapOfUsage) {
      this.mapOfUsage = mapOfUsage;
    }

    @Override
    public void run() {
      synchronized (mapOfUsage) {
        Long numberOfThreads=mapOfUsage.get(Thread.currentThread().getId());
        if(numberOfThreads==null){
          mapOfUsage.put(Thread.currentThread().getId(), 1l);
        }else{
          mapOfUsage.put(Thread.currentThread().getId(), ++numberOfThreads);
        }
      }

    }
  }

  public static void main(String[] args) {
    ThreadFactory threadFactory = Executors.defaultThreadFactory();
    final Map<Long, Long> mapOfUsage = new HashMap<>(AMOUNT * 2);
    ExecutorService service = Executors.newCachedThreadPool();
    for(int i=0;i<AMOUNT;i++){
      service.execute(threadFactory.newThread(new TestThread(mapOfUsage)));
    }
    for(Map.Entry<Long, Long> entry: mapOfUsage.entrySet()){
      if(entry.getValue()>1){
        System.out.println("Thread with id "+entry.getKey() +" was used "+entry.getValue()+ " times");
      }
    }
  }
}

:

9 27198

11 1810

13 1294

15 3347

17 6709

19 7259

21 39335

23 13552

25 535

27 19533

29 113495

31 62713

35 94103

33 53641

5328 18922

16388 28501

16384 114677

16386 39

Thread id 16698 60450

123096 19944

123102 60961

123115 24246

275492 108399

275490 11973

380143 10433

363358 55989

692626 6016

909079 25782

965801 32

948919 1782

Thread with id 948872 has been used 4 times

Thread id 938802 was used 24 times

Thread with id 923558 was used 7302 times

+1
source

All Articles