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

prevent synchronized blocking the virtual threads in JDK21 #5629

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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