Skip to content

Commit

Permalink
Merge pull request #474 from maiken2051/master
Browse files Browse the repository at this point in the history
Query with timer can throw IllegalThreadStateException - This PR makes sure the ThreadGroup is not destroyed before creating a new timer thread.
  • Loading branch information
cheenamalhotra authored Oct 12, 2017
2 parents 7a0fe66 + 9da95c5 commit 06cb754
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -7119,13 +7120,21 @@ final class TimeoutTimer implements Runnable {
private volatile Future<?> task;

private static final ExecutorService executor = Executors.newCachedThreadPool(new ThreadFactory() {
private final ThreadGroup tg = new ThreadGroup(threadGroupName);
private final String threadNamePrefix = tg.getName() + "-";
private final AtomicReference<ThreadGroup> tgr = new AtomicReference<>();
private final AtomicInteger threadNumber = new AtomicInteger(0);

@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(tg, r, threadNamePrefix + threadNumber.incrementAndGet());
public Thread newThread(Runnable r)
{
ThreadGroup tg = tgr.get();

if (tg == null || tg.isDestroyed())
{
tg = new ThreadGroup(threadGroupName);
tgr.set(tg);
}

Thread t = new Thread(tg, r, tg.getName() + "-" + threadNumber.incrementAndGet());
t.setDaemon(true);
return t;
}
Expand Down

0 comments on commit 06cb754

Please sign in to comment.