[infinispan-commits] Infinispan SVN: r435 - in trunk/core/src: test/java/org/infinispan/util and 1 other directories.
infinispan-commits at lists.jboss.org
infinispan-commits at lists.jboss.org
Wed Jun 10 22:51:56 EDT 2009
Author: manik.surtani at jboss.com
Date: 2009-06-10 22:51:56 -0400 (Wed, 10 Jun 2009)
New Revision: 435
Added:
trunk/core/src/main/java/org/infinispan/util/concurrent/ValueNotifier.java
trunk/core/src/test/java/org/infinispan/util/concurrent/
trunk/core/src/test/java/org/infinispan/util/concurrent/ValueNotifierTest.java
Log:
Added value notifier to watch for value changes, based on AQS
Added: trunk/core/src/main/java/org/infinispan/util/concurrent/ValueNotifier.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/util/concurrent/ValueNotifier.java (rev 0)
+++ trunk/core/src/main/java/org/infinispan/util/concurrent/ValueNotifier.java 2009-06-11 02:51:56 UTC (rev 435)
@@ -0,0 +1,56 @@
+package org.infinispan.util.concurrent;
+
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.AbstractQueuedSynchronizer;
+
+/**
+ * Allows threads to watch a variable and be notified when the state of the variable reaches a specific value.
+ * <p/>
+ * E.g.,
+ * <p/>
+ * ValueNotifier v = new ValueNotifier(0);
+ * <p/>
+ * Thread1:
+ * <p/>
+ * v.setValue(10);
+ * <p/>
+ * Thread2:
+ * <p/>
+ * v.awaitValue(5); // will block until another thread sets the value to 5
+ *
+ * @author Manik Surtani
+ * @since 4.0
+ */
+public class ValueNotifier extends AbstractQueuedSynchronizer {
+ private static final long serialVersionUID = 1744280161777661090l;
+
+ public ValueNotifier(int initValue) {
+ setState(initValue);
+ }
+
+ @Override
+ public final int tryAcquireShared(int value) {
+ // return 1 if we allow the requestor to proceed, -1 if we want the requestor to block.
+ return getState() == value ? 1 : -1;
+ }
+
+ @Override
+ public final boolean tryReleaseShared(int state) {
+ // used as a mechanism to set the state of the Sync.
+ setState(state);
+ return true;
+ }
+
+ public final void setValue(int value) {
+ // do not use setState() directly since this won't notify parked threads.
+ releaseShared(value);
+ }
+
+ public final void awaitValue(int value) throws InterruptedException {
+ acquireSharedInterruptibly(value);
+ }
+
+ public final boolean awaitValue(int value, long time, TimeUnit unit) throws InterruptedException {
+ return tryAcquireSharedNanos(value, unit.toNanos(time));
+ }
+}
Property changes on: trunk/core/src/main/java/org/infinispan/util/concurrent/ValueNotifier.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Added: trunk/core/src/test/java/org/infinispan/util/concurrent/ValueNotifierTest.java
===================================================================
--- trunk/core/src/test/java/org/infinispan/util/concurrent/ValueNotifierTest.java (rev 0)
+++ trunk/core/src/test/java/org/infinispan/util/concurrent/ValueNotifierTest.java 2009-06-11 02:51:56 UTC (rev 435)
@@ -0,0 +1,65 @@
+package org.infinispan.util.concurrent;
+
+import org.testng.annotations.Test;
+
+import java.util.LinkedList;
+import java.util.List;
+
+ at Test(groups = "unit")
+public class ValueNotifierTest {
+ public void testNotifier() throws InterruptedException {
+ final ValueNotifier vn = new ValueNotifier(10);
+ final List<Integer> threadsCompleted = new LinkedList<Integer>();
+
+
+ Thread t1 = new Thread() {
+ public void run() {
+ try {
+ vn.awaitValue(50);
+ threadsCompleted.add(1);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+ };
+
+ Thread t2 = new Thread() {
+ public void run() {
+ try {
+ vn.awaitValue(50);
+ threadsCompleted.add(2);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+ };
+
+ Thread t3 = new Thread() {
+ public void run() {
+ try {
+ vn.awaitValue(40);
+ threadsCompleted.add(3);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+ };
+
+ t1.start();
+ t2.start();
+ t3.start();
+
+ Thread.sleep(100);
+ assert threadsCompleted.isEmpty();
+ vn.setValue(50);
+ Thread.sleep(100);
+ assert threadsCompleted.size() == 2;
+ assert threadsCompleted.contains(1);
+ assert threadsCompleted.contains(2);
+
+ vn.setValue(40);
+ Thread.sleep(100);
+ assert threadsCompleted.size() == 3;
+ assert threadsCompleted.contains(3);
+ }
+}
Property changes on: trunk/core/src/test/java/org/infinispan/util/concurrent/ValueNotifierTest.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
More information about the infinispan-commits
mailing list