From 2e52a797d0ecee53d9b38720da08ffea9a413c47 Mon Sep 17 00:00:00 2001 From: Peter Veentjer Date: Tue, 14 Jan 2025 15:09:51 +0200 Subject: [PATCH] Deprecated ThreadHints. (#312) Using Thread.onSpinWait directly. --- .../concurrent/BackoffIdleStrategy.java | 4 +-- .../concurrent/BusySpinIdleStrategy.java | 6 ++-- .../concurrent/ControllableIdleStrategy.java | 5 ++- .../ManyToManyConcurrentArrayQueue.java | 7 ++--- .../concurrent/SnowflakeIdGenerator.java | 6 ++-- .../java/org/agrona/hints/ThreadHints.java | 3 ++ .../org/agrona/hints/ThreadHintsTest.java | 31 ------------------- 7 files changed, 13 insertions(+), 49 deletions(-) delete mode 100644 agrona/src/test/java/org/agrona/hints/ThreadHintsTest.java diff --git a/agrona/src/main/java/org/agrona/concurrent/BackoffIdleStrategy.java b/agrona/src/main/java/org/agrona/concurrent/BackoffIdleStrategy.java index 48e1bd123..2c6646c74 100644 --- a/agrona/src/main/java/org/agrona/concurrent/BackoffIdleStrategy.java +++ b/agrona/src/main/java/org/agrona/concurrent/BackoffIdleStrategy.java @@ -15,8 +15,6 @@ */ package org.agrona.concurrent; -import org.agrona.hints.ThreadHints; - import java.util.concurrent.locks.LockSupport; @SuppressWarnings("unused") @@ -201,7 +199,7 @@ public void idle() break; case SPINNING: - ThreadHints.onSpinWait(); + Thread.onSpinWait(); if (++spins > maxSpins) { state = YIELDING; diff --git a/agrona/src/main/java/org/agrona/concurrent/BusySpinIdleStrategy.java b/agrona/src/main/java/org/agrona/concurrent/BusySpinIdleStrategy.java index 0559d84bd..705c3427d 100644 --- a/agrona/src/main/java/org/agrona/concurrent/BusySpinIdleStrategy.java +++ b/agrona/src/main/java/org/agrona/concurrent/BusySpinIdleStrategy.java @@ -15,8 +15,6 @@ */ package org.agrona.concurrent; -import org.agrona.hints.ThreadHints; - /** * Busy spin strategy targeted at lowest possible latency. This strategy will monopolise a thread to achieve the lowest * possible latency. Useful for creating bubbles in the execution pipeline of tight busy spin loops with no other logic @@ -53,7 +51,7 @@ public void idle(final int workCount) return; } - ThreadHints.onSpinWait(); + Thread.onSpinWait(); } /** @@ -61,7 +59,7 @@ public void idle(final int workCount) */ public void idle() { - ThreadHints.onSpinWait(); + Thread.onSpinWait(); } /** diff --git a/agrona/src/main/java/org/agrona/concurrent/ControllableIdleStrategy.java b/agrona/src/main/java/org/agrona/concurrent/ControllableIdleStrategy.java index 5818529c0..06cccb02d 100644 --- a/agrona/src/main/java/org/agrona/concurrent/ControllableIdleStrategy.java +++ b/agrona/src/main/java/org/agrona/concurrent/ControllableIdleStrategy.java @@ -16,13 +16,12 @@ package org.agrona.concurrent; import org.agrona.concurrent.status.StatusIndicatorReader; -import org.agrona.hints.ThreadHints; import java.util.concurrent.locks.LockSupport; /** * {@link IdleStrategy} which can be controlled by a counter so its mode of operation can be switched between - * doing nothing (NOOP), busy spinning by calling {@link ThreadHints#onSpinWait()}, yielding by calling + * doing nothing (NOOP), busy spinning by calling {@link Thread#onSpinWait()}, yielding by calling * {@link Thread#yield()}, or sleeping for the minimum period by calling {@link LockSupport#parkNanos(long)} when * work count is zero, so it idles. */ @@ -101,7 +100,7 @@ public void idle() break; case BUSY_SPIN: - ThreadHints.onSpinWait(); + Thread.onSpinWait(); break; case YIELD: diff --git a/agrona/src/main/java/org/agrona/concurrent/ManyToManyConcurrentArrayQueue.java b/agrona/src/main/java/org/agrona/concurrent/ManyToManyConcurrentArrayQueue.java index e50ce33f1..7557a534f 100644 --- a/agrona/src/main/java/org/agrona/concurrent/ManyToManyConcurrentArrayQueue.java +++ b/agrona/src/main/java/org/agrona/concurrent/ManyToManyConcurrentArrayQueue.java @@ -16,7 +16,6 @@ package org.agrona.concurrent; import org.agrona.UnsafeApi; -import org.agrona.hints.ThreadHints; import java.util.Collection; import java.util.function.Consumer; @@ -109,7 +108,7 @@ public boolean offer(final E e) return true; } - ThreadHints.onSpinWait(); + Thread.onSpinWait(); } } @@ -145,7 +144,7 @@ public E poll() return (E)e; } - ThreadHints.onSpinWait(); + Thread.onSpinWait(); } } @@ -182,7 +181,7 @@ public E peek() } } - ThreadHints.onSpinWait(); + Thread.onSpinWait(); } } diff --git a/agrona/src/main/java/org/agrona/concurrent/SnowflakeIdGenerator.java b/agrona/src/main/java/org/agrona/concurrent/SnowflakeIdGenerator.java index a54640e06..3e2980360 100644 --- a/agrona/src/main/java/org/agrona/concurrent/SnowflakeIdGenerator.java +++ b/agrona/src/main/java/org/agrona/concurrent/SnowflakeIdGenerator.java @@ -15,8 +15,6 @@ */ package org.agrona.concurrent; -import org.agrona.hints.ThreadHints; - import java.util.concurrent.atomic.AtomicLongFieldUpdater; abstract class AbstractSnowflakeIdGeneratorPaddingLhs @@ -204,7 +202,7 @@ public long maxSequence() /** * Generate the next id in sequence. If {@link #maxSequence()} is reached within the same millisecond then this - * implementation will busy spin until the next millisecond using {@link ThreadHints#onSpinWait()} and checking + * implementation will busy spin until the next millisecond using {@link Thread#onSpinWait()} and checking * for {@link Thread#isInterrupted()}. * * @return the next unique id for this node. @@ -243,7 +241,7 @@ public long nextId() throw new IllegalStateException("unexpected thread interrupt"); } - ThreadHints.onSpinWait(); + Thread.onSpinWait(); } } diff --git a/agrona/src/main/java/org/agrona/hints/ThreadHints.java b/agrona/src/main/java/org/agrona/hints/ThreadHints.java index d231e0244..65353d3db 100644 --- a/agrona/src/main/java/org/agrona/hints/ThreadHints.java +++ b/agrona/src/main/java/org/agrona/hints/ThreadHints.java @@ -19,12 +19,15 @@ import org.agrona.SystemUtil; /** + * This class is deprecated. Use {@link Thread#onSpinWait()} directly. + *

* This class captures possible hints that may be used by some * runtimes to improve code performance. It is intended to capture hinting * behaviours that are implemented in or anticipated to be spec'ed under the * {@link java.lang.Thread} class in some Java SE versions, but missing in prior * versions. */ +@Deprecated public final class ThreadHints { /** diff --git a/agrona/src/test/java/org/agrona/hints/ThreadHintsTest.java b/agrona/src/test/java/org/agrona/hints/ThreadHintsTest.java deleted file mode 100644 index eac42b782..000000000 --- a/agrona/src/test/java/org/agrona/hints/ThreadHintsTest.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2014-2025 Real Logic Limited. - * Copyright 2016 Gil Tene - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.agrona.hints; - -import org.junit.jupiter.api.Test; - -class ThreadHintsTest -{ - @Test - void shouldDoNothingOnSpinWait() - { - for (int i = 0; i < 100; i++) - { - ThreadHints.onSpinWait(); - } - } -}