Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / mcr-test / src / edu / tamu / aser / rvtest_simple_tests / JavaUtilConcurrentTests.java
diff --git a/JMCR-Stable/mcr-test/src/edu/tamu/aser/rvtest_simple_tests/JavaUtilConcurrentTests.java b/JMCR-Stable/mcr-test/src/edu/tamu/aser/rvtest_simple_tests/JavaUtilConcurrentTests.java
new file mode 100644 (file)
index 0000000..7c6f4d5
--- /dev/null
@@ -0,0 +1,48 @@
+package edu.tamu.aser.rvtest_simple_tests;
+
+import java.util.concurrent.locks.ReentrantLock;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import edu.tamu.aser.reexecution.JUnit4MCRRunner;
+
+@RunWith(JUnit4MCRRunner.class)
+public class JavaUtilConcurrentTests {
+
+    private int counter;
+
+    @Test
+    public void testReentrantLock() throws InterruptedException {
+        counter = 0;
+        ReentrantLock lock = new ReentrantLock();
+        Thread t1 = new Thread(new LockProtectedIncrement(lock));
+        Thread t2 = new Thread(new LockProtectedIncrement(lock));
+        t1.start();
+        t2.start();
+        t1.join();
+        t2.join();
+        assert 2 == counter;
+    }
+
+    private final class LockProtectedIncrement implements Runnable {
+
+        private final ReentrantLock lock;
+
+        public LockProtectedIncrement(ReentrantLock lock) {
+            this.lock = lock;
+        }
+
+        @Override
+        public void run() {
+            lock.lock();
+            try {
+                counter++;
+            } finally {
+                lock.unlock();
+            }
+        }
+    }
+
+
+}