Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Executor service instead of thread #162

Merged
16 changes: 10 additions & 6 deletions src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
import java.util.Set;
import java.util.SimpleTimeZone;
import java.util.TimeZone;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -6824,7 +6826,7 @@ final void TryProcessFeatureExtAck(boolean featureExtAckReceived) throws SQLServ
final class TimeoutTimer implements Runnable {
private final int timeoutSeconds;
private final TDSCommand command;
private Thread timerThread;
private ExecutorService executor = Executors.newCachedThreadPool();
private volatile boolean canceled = false;

TimeoutTimer(int timeoutSeconds,
Expand All @@ -6837,17 +6839,19 @@ final class TimeoutTimer implements Runnable {
}

final void start() {
timerThread = new Thread(this);
timerThread.setDaemon(true);
timerThread.start();
//if ExecutorService is shutdown already, reset it
if(executor.isShutdown()){
executor = Executors.newCachedThreadPool();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The executor does not create daemon Threads by default, thus the result may be a little different.

I would probably use a thread factory as a parameter that use daemon threads.

}
executor.execute(this);
}

final void stop() {
executor.shutdownNow();
canceled = true;
timerThread.interrupt();
}

public void run() {
public void run() {
int secondsRemaining = timeoutSeconds;
try {
// Poll every second while time is left on the timer.
Expand Down