Skip to content

Commit eeb3d02

Browse files
committed
Move AffinitySupport.setThreadId() to Affinity.
1 parent 8db13e3 commit eeb3d02

2 files changed

Lines changed: 20 additions & 17 deletions

File tree

affinity/src/main/java/net/openhft/affinity/Affinity.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import java.io.PrintWriter;
2525
import java.io.StringWriter;
26+
import java.lang.reflect.Field;
2627
import java.util.BitSet;
2728

2829
/**
@@ -32,9 +33,9 @@
3233
*/
3334
public enum Affinity {
3435
;
36+
static final Logger LOGGER = LoggerFactory.getLogger(Affinity.class);
3537
@NotNull
3638
private static final IAffinity AFFINITY_IMPL;
37-
static final Logger LOGGER = LoggerFactory.getLogger(Affinity.class);
3839
private static Boolean JNAAvailable;
3940

4041
static {
@@ -152,16 +153,16 @@ public static BitSet getAffinity() {
152153
return AFFINITY_IMPL.getAffinity();
153154
}
154155

155-
public static void setAffinity(final BitSet affinity) {
156-
AFFINITY_IMPL.setAffinity(affinity);
157-
}
158-
159156
public static void setAffinity(int cpu) {
160157
BitSet affinity = new BitSet(Runtime.getRuntime().availableProcessors());
161158
affinity.set(cpu);
162159
setAffinity(affinity);
163160
}
164161

162+
public static void setAffinity(final BitSet affinity) {
163+
AFFINITY_IMPL.setAffinity(affinity);
164+
}
165+
165166
public static int getCpu() {
166167
return AFFINITY_IMPL.getCpu();
167168
}
@@ -170,6 +171,19 @@ public static int getThreadId() {
170171
return AFFINITY_IMPL.getThreadId();
171172
}
172173

174+
public static void setThreadId() {
175+
try {
176+
int threadId = Affinity.getThreadId();
177+
final Field tid = Thread.class.getDeclaredField("tid");
178+
tid.setAccessible(true);
179+
final Thread thread = Thread.currentThread();
180+
tid.setLong(thread, threadId);
181+
Affinity.LOGGER.info("Set {} to thread id {}", thread.getName(), threadId);
182+
} catch (Exception e) {
183+
throw new IllegalStateException(e);
184+
}
185+
}
186+
173187
public static boolean isJNAAvailable() {
174188
if (JNAAvailable == null)
175189
try {

affinity/src/main/java/net/openhft/affinity/AffinitySupport.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package net.openhft.affinity;
1818

19-
import java.lang.reflect.Field;
20-
2119
/**
2220
* For backward compatibility with Affinity 2.x
2321
*/
@@ -28,16 +26,7 @@ public static int getThreadId() {
2826
return Affinity.getThreadId();
2927
}
3028
public static void setThreadId() {
31-
try {
32-
int threadId = Affinity.getThreadId();
33-
final Field tid = Thread.class.getDeclaredField("tid");
34-
tid.setAccessible(true);
35-
final Thread thread = Thread.currentThread();
36-
tid.setLong(thread, threadId);
37-
Affinity.LOGGER.info("Set {} to thread id {}", thread.getName(), threadId);
38-
} catch (Exception e) {
39-
throw new IllegalStateException(e);
40-
}
29+
Affinity.setThreadId();
4130
}
4231

4332
}

0 commit comments

Comments
 (0)