I just had a similar case, so I give a later answer.
The problem is that your solution with Thread.isAlive() and Thread.start() not multithreaded. It may happen that the first thread calls your code, executes isAlive() and somewhere inside Thread.start() , after a new thread has been started and before its state has been changed, a task switch appears, and the second one calls executes isAlive() when it is still false , causing start() called twice. To make it worse somewhere inside start() , it seems that the task switch is forced and therefore this problem occurs quite often.
Decision. Override start() to make it multithreaded, e.g.
private final AtomicBoolean started = new AtomicBoolean(false); @Override public synchronized void start() { if (!started.getAndSet(true)) { super.start(); } }
Then your problem will no longer appear, even if start() accidentally called twice.
Johanna
source share