[infinispan-commits] Infinispan SVN: r447 - in trunk/core/src: test/java/org/infinispan/util/concurrent and 1 other directory.
infinispan-commits at lists.jboss.org
infinispan-commits at lists.jboss.org
Fri Jun 12 07:06:59 EDT 2009
Author: manik.surtani at jboss.com
Date: 2009-06-12 07:06:59 -0400 (Fri, 12 Jun 2009)
New Revision: 447
Added:
trunk/core/src/main/java/org/infinispan/util/concurrent/WatchableValue.java
trunk/core/src/test/java/org/infinispan/util/concurrent/WatchableValueTest.java
Removed:
trunk/core/src/main/java/org/infinispan/util/concurrent/ValueNotifier.java
trunk/core/src/test/java/org/infinispan/util/concurrent/ValueNotifierTest.java
Log:
Utility to be able to watch for value changes
Deleted: trunk/core/src/main/java/org/infinispan/util/concurrent/ValueNotifier.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/util/concurrent/ValueNotifier.java 2009-06-12 11:02:38 UTC (rev 446)
+++ trunk/core/src/main/java/org/infinispan/util/concurrent/ValueNotifier.java 2009-06-12 11:06:59 UTC (rev 447)
@@ -1,56 +0,0 @@
-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));
- }
-}
Copied: trunk/core/src/main/java/org/infinispan/util/concurrent/WatchableValue.java (from rev 439, trunk/core/src/main/java/org/infinispan/util/concurrent/ValueNotifier.java)
===================================================================
--- trunk/core/src/main/java/org/infinispan/util/concurrent/WatchableValue.java (rev 0)
+++ trunk/core/src/main/java/org/infinispan/util/concurrent/WatchableValue.java 2009-06-12 11:06:59 UTC (rev 447)
@@ -0,0 +1,60 @@
+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 WatchableValue extends AbstractQueuedSynchronizer {
+ private static final long serialVersionUID = 1744280161777661090l;
+
+ public WatchableValue(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));
+ }
+
+ public int getValue() {
+ return getState();
+ }
+}
Property changes on: trunk/core/src/main/java/org/infinispan/util/concurrent/WatchableValue.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Deleted: trunk/core/src/test/java/org/infinispan/util/concurrent/ValueNotifierTest.java
===================================================================
--- trunk/core/src/test/java/org/infinispan/util/concurrent/ValueNotifierTest.java 2009-06-12 11:02:38 UTC (rev 446)
+++ trunk/core/src/test/java/org/infinispan/util/concurrent/ValueNotifierTest.java 2009-06-12 11:06:59 UTC (rev 447)
@@ -1,65 +0,0 @@
-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);
- }
-}
Copied: trunk/core/src/test/java/org/infinispan/util/concurrent/WatchableValueTest.java (from rev 439, trunk/core/src/test/java/org/infinispan/util/concurrent/ValueNotifierTest.java)
===================================================================
--- trunk/core/src/test/java/org/infinispan/util/concurrent/WatchableValueTest.java (rev 0)
+++ trunk/core/src/test/java/org/infinispan/util/concurrent/WatchableValueTest.java 2009-06-12 11:06:59 UTC (rev 447)
@@ -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 WatchableValueTest {
+ public void testNotifier() throws InterruptedException {
+ final WatchableValue vn = new WatchableValue(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/WatchableValueTest.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
More information about the infinispan-commits
mailing list