Skip to content

Commit 8e9bd6f

Browse files
fengxue-ISNathan Henderson
andcommitted
Update getState() API to use holder.threadStatus field
Co-authored-by: Nathan Henderson <nathan.henderson@ibm.com> Signed-off-by: Jack Lu <Jack.S.Lu@ibm.com>
1 parent 5d34738 commit 8e9bd6f

File tree

1 file changed

+44
-6
lines changed

1 file changed

+44
-6
lines changed

src/java.base/share/classes/java/lang/Thread.java

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
/*
2727
* ===========================================================================
28-
* (c) Copyright IBM Corp. 2021, 2024 All Rights Reserved
28+
* (c) Copyright IBM Corp. 2021, 2025 All Rights Reserved
2929
* ===========================================================================
3030
*/
3131

@@ -2426,21 +2426,59 @@ public State getState() {
24262426
return threadState();
24272427
}
24282428

2429+
/**
2430+
* Returns the translation from a J9VMThread state to a Thread::State.
2431+
*
2432+
* @return this thread's state.
2433+
*
2434+
* @see State
2435+
*/
2436+
private State translateJ9VMThreadStateToThreadState(int status) {
2437+
switch (status) {
2438+
case 1: // J9VMTHREAD_STATE_RUNNING
2439+
return State.RUNNABLE;
2440+
case 2: // J9VMTHREAD_STATE_BLOCKED
2441+
return State.BLOCKED;
2442+
case 4: // J9VMTHREAD_STATE_WAITING
2443+
case 0x80: // J9VMTHREAD_STATE_PARKED
2444+
return State.WAITING;
2445+
case 8: // J9VMTHREAD_STATE_SLEEPING
2446+
case 64: // J9VMTHREAD_STATE_WAITING_TIMED
2447+
case 0x100: // J9VMTHREAD_STATE_PARKED_TIMED
2448+
return State.TIMED_WAITING;
2449+
case 32: // J9VMTHREAD_STATE_DEAD
2450+
return State.TERMINATED;
2451+
default:
2452+
synchronized (interruptLock) {
2453+
if (eetop == NO_REF) {
2454+
return State.TERMINATED;
2455+
}
2456+
return State.values()[getStateImpl(eetop)];
2457+
}
2458+
}
2459+
}
2460+
24292461
/**
24302462
* Returns the state of this thread.
24312463
* This method can be used instead of getState as getState is not final and
24322464
* so can be overridden to run arbitrary code.
24332465
*/
24342466
State threadState() {
2435-
synchronized (interruptLock) {
2467+
if (started) {
24362468
if (eetop == NO_REF) {
2437-
if (isDead()) {
2438-
return State.TERMINATED;
2469+
return State.TERMINATED;
2470+
}
2471+
if (holder == null) {
2472+
synchronized (interruptLock) {
2473+
if (eetop == NO_REF) {
2474+
return State.TERMINATED;
2475+
}
2476+
return State.values()[getStateImpl(eetop)];
24392477
}
2440-
return State.NEW;
24412478
}
2442-
return State.values()[getStateImpl(eetop)];
2479+
return translateJ9VMThreadStateToThreadState(holder.threadStatus);
24432480
}
2481+
return State.NEW;
24442482
}
24452483

24462484
/**

0 commit comments

Comments
 (0)