You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2019/05/22 12:18:13 UTC

[commons-rng] 03/04: synchronized at the block level.

This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-rng.git

commit d4bced685ecb6dc84dae9add1893562b3853b646
Author: aherbert <ah...@apache.org>
AuthorDate: Wed May 22 12:25:43 2019 +0100

    synchronized at the block level.
---
 .../apache/commons/rng/simple/JDKRandomBridge.java | 44 ++++++++++++----------
 1 file changed, 25 insertions(+), 19 deletions(-)

diff --git a/commons-rng-simple/src/main/java/org/apache/commons/rng/simple/JDKRandomBridge.java b/commons-rng-simple/src/main/java/org/apache/commons/rng/simple/JDKRandomBridge.java
index 1118d4e..aa13a25 100644
--- a/commons-rng-simple/src/main/java/org/apache/commons/rng/simple/JDKRandomBridge.java
+++ b/commons-rng-simple/src/main/java/org/apache/commons/rng/simple/JDKRandomBridge.java
@@ -65,14 +65,16 @@ public final class JDKRandomBridge extends Random {
 
     /** {@inheritDoc} */
     @Override
-    public synchronized void setSeed(long seed) {
-        if (isInitialized) {
-            delegate = RandomSource.create(source, seed);
+    public void setSeed(long seed) {
+        synchronized (this) {
+            if (isInitialized) {
+                delegate = RandomSource.create(source, seed);
 
-            // Force the clearing of the "haveNextNextGaussian" flag
-            // (cf. Javadoc of the base class); the value passed here
-            // is irrelevant (since it will not be used).
-            super.setSeed(0L);
+                // Force the clearing of the "haveNextNextGaussian" flag
+                // (cf. Javadoc of the base class); the value passed here
+                // is irrelevant (since it will not be used).
+                super.setSeed(0L);
+            }
         }
     }
 
@@ -90,37 +92,41 @@ public final class JDKRandomBridge extends Random {
      * pseudo-random 32-bits integer.
      */
     @Override
-    protected synchronized int next(int n) {
-        return delegate.nextInt() >>> (32 - n);
+    protected int next(int n) {
+        synchronized (this) {
+            return delegate.nextInt() >>> (32 - n);
+        }
     }
 
     /**
-     * @param out Output stream.
+     * @param output Output stream.
      * @throws IOException if an error occurs.
      */
-    private synchronized void writeObject(ObjectOutputStream out)
+    private void writeObject(ObjectOutputStream output)
         throws IOException {
-        // Write non-transient fields.
-        out.defaultWriteObject();
+        synchronized (this) {
+            // Write non-transient fields.
+            output.defaultWriteObject();
 
-        // Save current state.
-        out.writeObject(((RandomProviderDefaultState) delegate.saveState()).getState());
+            // Save current state.
+            output.writeObject(((RandomProviderDefaultState) delegate.saveState()).getState());
+        }
     }
 
     /**
-     * @param in Input stream.
+     * @param input Input stream.
      * @throws IOException if an error occurs.
      * @throws ClassNotFoundException if an error occurs.
      */
-    private void readObject(ObjectInputStream in)
+    private void readObject(ObjectInputStream input)
         throws IOException,
                ClassNotFoundException {
         // Read non-transient fields.
-        in.defaultReadObject();
+        input.defaultReadObject();
 
         // Recreate the "delegate" from serialized info.
         delegate = RandomSource.create(source);
         // And restore its state.
-        delegate.restoreState(new RandomProviderDefaultState((byte[]) in.readObject()));
+        delegate.restoreState(new RandomProviderDefaultState((byte[]) input.readObject()));
     }
 }