Skip to content

Commit

Permalink
use AtomicLong instead of a Lock
Browse files Browse the repository at this point in the history
  • Loading branch information
jono-coder committed Oct 6, 2022
1 parent b75dd6e commit e8174f4
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
Expand Down Expand Up @@ -2038,7 +2039,7 @@ else if (null != (trustStoreFileName = System.getProperty("javax.net.ssl.trustSt

/**
* Attempts to poll the input stream to see if the network socket is still connected.
*
*
* @return
*/
final Boolean networkSocketStillConnected() {
Expand Down Expand Up @@ -3173,8 +3174,7 @@ final class SocketConnector implements Runnable {

// a counter used to give unique IDs to each connector thread.
// this will have the id of the thread that was last created.
private static long lastThreadID = 0;
private static final Lock LOCK = new ReentrantLock();
private static final AtomicLong lastThreadID = new AtomicLong();

/**
* Constructs a new SocketConnector object with the associated socket and socketFinder
Expand Down Expand Up @@ -3233,19 +3233,15 @@ public String toString() {
* Generates the next unique thread id.
*/
private static long nextThreadID() {
LOCK.lock();
try {
if (lastThreadID == Long.MAX_VALUE) {
return lastThreadID.updateAndGet(threadId -> {
if (threadId == Long.MAX_VALUE) {
if (logger.isLoggable(Level.FINER))
logger.finer("Resetting the Id count");
lastThreadID = 1;
return 1;
} else {
lastThreadID++;
return threadId + 1;
}
return lastThreadID;
} finally {
LOCK.unlock();
}
});
}
}

Expand Down

0 comments on commit e8174f4

Please sign in to comment.