You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by st...@apache.org on 2012/11/19 17:04:31 UTC
svn commit: r1411269 - in /myfaces/core/branches/2.2.x/impl/src:
main/java/org/apache/myfaces/util/ThreadsafeXorShiftRandom.java
main/java/org/apache/myfaces/util/XorShiftRandom.java
test/java/org/apache/myfaces/util/XorShiftRandomTest.java
Author: struberg
Date: Mon Nov 19 16:04:30 2012
New Revision: 1411269
URL: http://svn.apache.org/viewvc?rev=1411269&view=rev
Log:
MYFACES-3654 implement fast threadsafe Random algorithm.
Added:
myfaces/core/branches/2.2.x/impl/src/main/java/org/apache/myfaces/util/ThreadsafeXorShiftRandom.java
myfaces/core/branches/2.2.x/impl/src/main/java/org/apache/myfaces/util/XorShiftRandom.java (with props)
myfaces/core/branches/2.2.x/impl/src/test/java/org/apache/myfaces/util/XorShiftRandomTest.java
Added: myfaces/core/branches/2.2.x/impl/src/main/java/org/apache/myfaces/util/ThreadsafeXorShiftRandom.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.2.x/impl/src/main/java/org/apache/myfaces/util/ThreadsafeXorShiftRandom.java?rev=1411269&view=auto
==============================================================================
--- myfaces/core/branches/2.2.x/impl/src/main/java/org/apache/myfaces/util/ThreadsafeXorShiftRandom.java (added)
+++ myfaces/core/branches/2.2.x/impl/src/main/java/org/apache/myfaces/util/ThreadsafeXorShiftRandom.java Mon Nov 19 16:04:30 2012
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.util;
+
+import java.util.Random;
+
+/**
+ * A threadsafe implementation of {@link XorShiftRandom}.
+ * We use a ThreadLocal to give each thread it's own implementation.
+ */
+public class ThreadsafeXorShiftRandom extends XorShiftRandom
+{
+ /**
+ * Only used for creating the initial seed for each thread!
+ *
+ * We use a threadsafe java.util.Random for making sure that
+ * each thread get it's very own initial seed value.
+ *
+ * If we would not do this then starting dozen Threads at the same time
+ * would result in all having the same seed and thus generate
+ * the same random numbers.
+ */
+ private static Random seedRandom = new Random(System.nanoTime());
+
+ public ThreadsafeXorShiftRandom()
+ {
+ // seed is not used but created by the ThreadLocal
+ super(0);
+ }
+
+ private static final ThreadLocal<XorShiftRandom> random = new ThreadLocal<XorShiftRandom>()
+ {
+ @Override
+ protected XorShiftRandom initialValue()
+ {
+ return new XorShiftRandom(seedRandom.nextLong());
+ }
+ };
+
+ /**
+ * We use the random generator for this very thread.
+ * This method is perfectly threadsafe. It is also guaranteed
+ * that each thread will get own values.
+ */
+ @Override
+ public long random()
+ {
+ return random.get().random();
+ }
+}
Added: myfaces/core/branches/2.2.x/impl/src/main/java/org/apache/myfaces/util/XorShiftRandom.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.2.x/impl/src/main/java/org/apache/myfaces/util/XorShiftRandom.java?rev=1411269&view=auto
==============================================================================
--- myfaces/core/branches/2.2.x/impl/src/main/java/org/apache/myfaces/util/XorShiftRandom.java (added)
+++ myfaces/core/branches/2.2.x/impl/src/main/java/org/apache/myfaces/util/XorShiftRandom.java Mon Nov 19 16:04:30 2012
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.util;
+
+/**
+ * A simple XORShift Random generator.
+ * This class is NOT synchronized, you need to do that yourself if needed!
+ */
+public class XorShiftRandom
+{
+ private long value;
+
+ public XorShiftRandom( long initialSeed )
+ {
+ value = initialSeed;
+ }
+
+ /**
+ * Calculate a random value based on the previous value stored in this instance.
+ * If you use this method in a concurring way, you need to synchronize the access!
+ * @return
+ */
+ public long random()
+ {
+ value = random(value);
+ return value;
+ }
+
+ /**
+ * Calculate a random value based on the given start Value.
+ * @param randomStart the start value. Usually the result of the previous invocation
+ * @return the new random value.
+ */
+ public static long random(final long randomStart)
+ {
+ long randomValue = randomStart;
+ randomValue ^= (randomValue << 21);
+ randomValue ^= (randomValue >>> 35);
+ randomValue ^= (randomValue << 4);
+
+ return randomValue;
+ }
+}
Propchange: myfaces/core/branches/2.2.x/impl/src/main/java/org/apache/myfaces/util/XorShiftRandom.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: myfaces/core/branches/2.2.x/impl/src/test/java/org/apache/myfaces/util/XorShiftRandomTest.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.2.x/impl/src/test/java/org/apache/myfaces/util/XorShiftRandomTest.java?rev=1411269&view=auto
==============================================================================
--- myfaces/core/branches/2.2.x/impl/src/test/java/org/apache/myfaces/util/XorShiftRandomTest.java (added)
+++ myfaces/core/branches/2.2.x/impl/src/test/java/org/apache/myfaces/util/XorShiftRandomTest.java Mon Nov 19 16:04:30 2012
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.util;
+
+import org.junit.Test;
+import org.testng.Assert;
+
+import java.util.HashSet;
+import java.util.Set;
+
+public class XorShiftRandomTest
+{
+
+ @Test
+ public void testXorShiftRandom()
+ {
+ XorShiftRandom random = new XorShiftRandom(System.nanoTime());
+ Set<Long> randomVals = new HashSet<Long>(10);
+
+ int randomCount = 100;
+
+ for (int i = 0; i < randomCount; i ++)
+ {
+ randomVals.add(random.random());
+ }
+
+ // if the random generator is good then we had no collisions.
+ Assert.assertEquals(randomVals.size(), 100);
+ }
+
+
+ @Test
+ public void testThreadsafeXorShiftRandom()
+ {
+ XorShiftRandom random = new ThreadsafeXorShiftRandom();
+ Set<Long> randomVals = new HashSet<Long>(10);
+
+ int randomCount = 100;
+
+ for (int i = 0; i < randomCount; i ++)
+ {
+ randomVals.add(random.random());
+ }
+
+ // if the random generator is good then we had no collisions.
+ Assert.assertEquals(randomVals.size(), 100);
+ }
+}