Skip to content

Commit

Permalink
Remove synchronized blocks in Thread isAlive and isDead methods
Browse files Browse the repository at this point in the history
threadRef = NO_REF assignment will happen once, and similarly, the
started field is also set once during initialization.

A user of these methods will probably invoke them multiple times
until the desired result is achieved. A delay in getting the most
up-to-date value should not hinder the functionality of these
methods.

To sum up, removing synchronization in these methods improves perf
by reducing lock contention without hindering the functionality.

Related: eclipse-openj9#20414

Signed-off-by: Babneet Singh <[email protected]>
  • Loading branch information
babsingh committed Oct 25, 2024
1 parent 6544c13 commit 53b61f4
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions jcl/src/java.base/share/classes/java/lang/Thread.java
Original file line number Diff line number Diff line change
Expand Up @@ -746,10 +746,8 @@ public static boolean interrupted() {
* @see Thread#start
*/
public final boolean isAlive() {
synchronized (lock) {
/*[PR CMVC 88976] the Thread is alive until cleanup() is called */
return threadRef != NO_REF;
}
/*[PR CMVC 88976] the Thread is alive until cleanup() is called */
return threadRef != NO_REF;
}

/*[PR 1FJMO7Q] A Thread can be !isAlive() and still be in its ThreadGroup */
Expand All @@ -765,9 +763,7 @@ public final boolean isAlive() {
*/
private boolean isDead() {
// Has already started, is not alive anymore, and has been removed from the ThreadGroup
synchronized(lock) {
return started && threadRef == NO_REF;
}
return started && threadRef == NO_REF;
}

/**
Expand Down

0 comments on commit 53b61f4

Please sign in to comment.