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.

Reference to PR 1FJMO7Q has been removed and a comment related to
ThreadGroup has been updated since the code seems to have evolved
since the comments were written.

Related: eclipse-openj9#20414

Signed-off-by: Babneet Singh <[email protected]>
  • Loading branch information
babsingh committed Oct 25, 2024
1 parent 6544c13 commit 343d8f2
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions jcl/src/java.base/share/classes/java/lang/Thread.java
Original file line number Diff line number Diff line change
Expand Up @@ -746,13 +746,10 @@ 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 */
/**
* Answers <code>true</code> if the receiver has
* already died and been removed from the ThreadGroup
Expand All @@ -764,10 +761,8 @@ public final boolean isAlive() {
* @see Thread#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;
}
/* Has already started and is not alive anymore. */
return (started && (threadRef == NO_REF));
}

/**
Expand Down

0 comments on commit 343d8f2

Please sign in to comment.