diff --git a/src/jdk.jfr/share/classes/jdk/jfr/consumer/EventStream.java b/src/jdk.jfr/share/classes/jdk/jfr/consumer/EventStream.java index 3545604b7a0..eda0331ce23 100644 --- a/src/jdk.jfr/share/classes/jdk/jfr/consumer/EventStream.java +++ b/src/jdk.jfr/share/classes/jdk/jfr/consumer/EventStream.java @@ -242,6 +242,10 @@ public interface EventStream extends AutoCloseable { /** * Releases all resources associated with this stream. *
+ * If a stream is started, asynchronously or synchronously, it is stopped + * immediately or after the next flush. This method does NOT + * guarantee that all registered actions are completed before return. + *
* Closing a previously closed stream has no effect. */ void close(); @@ -320,6 +324,8 @@ public interface EventStream extends AutoCloseable { * Start processing of actions. *
* Actions are performed in the current thread. + *
+ * To stop the stream, use the {@code #close()} method. * * @throws IllegalStateException if the stream is already started or closed */ @@ -329,6 +335,8 @@ public interface EventStream extends AutoCloseable { * Start asynchronous processing of actions. *
* Actions are performed in a single separate thread. + *
+ * To stop the stream, use the {@code #close()} method. * * @throws IllegalStateException if the stream is already started or closed */ diff --git a/src/jdk.jfr/share/classes/jdk/jfr/consumer/RecordingStream.java b/src/jdk.jfr/share/classes/jdk/jfr/consumer/RecordingStream.java index 054770a8dcb..c8c82befd54 100644 --- a/src/jdk.jfr/share/classes/jdk/jfr/consumer/RecordingStream.java +++ b/src/jdk.jfr/share/classes/jdk/jfr/consumer/RecordingStream.java @@ -329,6 +329,32 @@ public final class RecordingStream implements AutoCloseable, EventStream { directoryStream.start(startNanos); } + /** + * Start asynchronous processing of actions. + *
+ * Actions are performed in a single separate thread. + *
+ * To stop the stream, use the {@code #close()} method. + *
+ * The following example prints the CPU usage for ten seconds. When + * the current thread leaves the try-with-resources block the + * stream is stopped/closed. + *
+ *
+ * try (var stream = new RecordingStream()) {
+ * stream.enable("jdk.CPULoad").withPeriod(Duration.ofSeconds(1));
+ * stream.onEvent("jdk.CPULoad", event -> {
+ * System.out.println(event);
+ * });
+ * stream.startAsync();
+ * Thread.sleep(10_000);
+ * }
+ *
+ *
+ *
+ * @throws IllegalStateException if the stream is already started or closed
+ *
+ */
@Override
public void startAsync() {
PlatformRecording pr = PrivateAccess.getInstance().getPlatformRecording(recording);