JDK-8283688 : Make ThreadLocalRandom a final class
  • Type: CSR
  • Component: core-libs
  • Sub-Component: java.util.concurrent
  • Priority: P4
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 19
  • Submitted: 2022-03-25
  • Updated: 2022-03-29
  • Resolved: 2022-03-26
Related Reports
CSR :  
Description
Summary
-------

Update the java.util.concurrent.ThreadLocalRandom to be a final class.

Problem
-------
The ThreadLocalRandom class has a sole private constructor:
```
private ThreadLocalRandom() {
...
```
So in essence, the ThreadLocalRandom class cannot have any sub class. Marking the ThreadLocalRandom as final would thus convey it more clearly that the class cannot be extended.


Solution
--------
Update the class to `final`.


Specification
-------------
The proposed change looks as follows:

```
--- a/src/java.base/share/classes/java/util/concurrent/ThreadLocalRandom.java
+++ b/src/java.base/share/classes/java/util/concurrent/ThreadLocalRandom.java
@@ -93,7 +93,7 @@ import jdk.internal.misc.VM;
...
-public class ThreadLocalRandom extends Random {
+public final class ThreadLocalRandom extends Random {
```


Comments
Moving to Approved.
26-03-2022

Note that changing a class from non-final to final or vice-versa affects the default serialVersionUID computation. However, ThreadLocalRandom declares a static value for serialVersionUID, so making the class final should NOT result in a change.
26-03-2022