Skip to content

Commit 100bf93

Browse files
Copilotjanbodnar
andcommitted
Fix thread safety issue in connection pool example
Co-authored-by: janbodnar <16985640+janbodnar@users.noreply.github.com>
1 parent 54cf345 commit 100bf93

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

virtualthreads-structuredconcurrency.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,15 +1935,15 @@ void main() throws Exception {
19351935

19361936
class ConnectionPool {
19371937
private final Semaphore semaphore;
1938-
private int connectionId = 0;
1938+
private final java.util.concurrent.atomic.AtomicInteger connectionId = new java.util.concurrent.atomic.AtomicInteger(0);
19391939

19401940
ConnectionPool(int size) {
19411941
this.semaphore = new Semaphore(size);
19421942
}
19431943

19441944
Connection acquire() throws InterruptedException {
19451945
semaphore.acquire();
1946-
return new Connection(++connectionId, this);
1946+
return new Connection(connectionId.incrementAndGet(), this);
19471947
}
19481948

19491949
void release() {

0 commit comments

Comments
 (0)