Skip to content

Commit a439bf2

Browse files
fengxue-ISNathan Henderson
and
Nathan Henderson
committed
Update getState() API to use holder.threadStatus field
Co-authored-by: Nathan Henderson <[email protected]> Co-authored-by: Jack Lu <[email protected]>
1 parent 2e6d682 commit a439bf2

File tree

1 file changed

+43
-5
lines changed

1 file changed

+43
-5
lines changed

Diff for: src/java.base/share/classes/java/lang/Thread.java

+43-5
Original file line numberDiff line numberDiff line change
@@ -2783,21 +2783,59 @@ public State getState() {
27832783
return threadState();
27842784
}
27852785

2786+
/**
2787+
* Returns the translation from a J9VMThread state to a Thread::State.
2788+
*
2789+
* @return this thread's state.
2790+
*
2791+
* @see State
2792+
*/
2793+
private State translateJ9VMThreadStateToThreadState(int status) {
2794+
switch (status) {
2795+
case 1: // J9VMTHREAD_STATE_RUNNING
2796+
return State.RUNNABLE;
2797+
case 2: // J9VMTHREAD_STATE_BLOCKED
2798+
return State.BLOCKED;
2799+
case 4: // J9VMTHREAD_STATE_WAITING
2800+
case 0x80: // J9VMTHREAD_STATE_PARKED
2801+
return State.WAITING;
2802+
case 8: // J9VMTHREAD_STATE_SLEEPING
2803+
case 64: // J9VMTHREAD_STATE_WAITING_TIMED
2804+
case 0x100: // J9VMTHREAD_STATE_PARKED_TIMED
2805+
return State.TIMED_WAITING;
2806+
case 32: // J9VMTHREAD_STATE_DEAD
2807+
return State.TERMINATED;
2808+
default:
2809+
synchronized (interruptLock) {
2810+
if (eetop == NO_REF) {
2811+
return State.TERMINATED;
2812+
}
2813+
return State.values()[getStateImpl(eetop)];
2814+
}
2815+
}
2816+
}
2817+
27862818
/**
27872819
* Returns the state of this thread.
27882820
* This method can be used instead of getState as getState is not final and
27892821
* so can be overridden to run arbitrary code.
27902822
*/
27912823
State threadState() {
2792-
synchronized (interruptLock) {
2824+
if (started) {
27932825
if (eetop == NO_REF) {
2794-
if (isDead()) {
2795-
return State.TERMINATED;
2826+
return State.TERMINATED;
2827+
}
2828+
if (holder == null) {
2829+
synchronized (interruptLock) {
2830+
if (eetop == NO_REF) {
2831+
return State.TERMINATED;
2832+
}
2833+
return State.values()[getStateImpl(eetop)];
27962834
}
2797-
return State.NEW;
27982835
}
2799-
return State.values()[getStateImpl(eetop)];
2836+
return translateJ9VMThreadStateToThreadState(holder.threadStatus);
28002837
}
2838+
return State.NEW;
28012839
}
28022840

28032841
/**

0 commit comments

Comments
 (0)