Skip to content

Commit

Permalink
prevent synchronized blocking the virtual threads in JDK21
Browse files Browse the repository at this point in the history
Signed-off-by: jansupol <jan.supol@oracle.com>
  • Loading branch information
jansupol committed Apr 29, 2024
1 parent 75a9755 commit a495d24
Showing 1 changed file with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -25,6 +25,7 @@

import org.glassfish.jersey.internal.LocalizationMessages;
import org.glassfish.jersey.internal.guava.Preconditions;
import org.glassfish.jersey.internal.util.JdkVersion;

/**
* A committing output stream with optional serialized entity buffering functionality
Expand Down Expand Up @@ -58,6 +59,8 @@
public final class CommittingOutputStream extends OutputStream {

private static final Logger LOGGER = Logger.getLogger(CommittingOutputStream.class.getName());
private static final boolean JDK_21 = JdkVersion.getJdkVersion().getMajor() > 20;

/**
* Null stream provider.
*/
Expand Down Expand Up @@ -275,7 +278,13 @@ private void flushBuffer(boolean endOfStream) throws IOException {

commitStream(currentSize);
if (buffer != null) {
buffer.writeTo(adaptedOutput);
if (adaptedOutput != null && JDK_21) {
adaptedOutput.write(buffer.toByteArray());
} else {
// Virtual thread in JDK 21 are blocked by synchronized writeTo
// but about 10% faster than ^ without virtual threads.
buffer.writeTo(adaptedOutput);
}
}
}
}
Expand Down

0 comments on commit a495d24

Please sign in to comment.