Examples of RendezvousBarrier


Examples of org.apache.commons.transaction.util.RendezvousBarrier

        final Map map1 = new HashMap();

        final TransactionalMapWrapper txMap1 = getNewWrapper(map1);

        final RendezvousBarrier beforeCommitBarrier =
            new RendezvousBarrier("Before Commit", 2, BARRIER_TIMEOUT, sLogger);

        final RendezvousBarrier afterCommitBarrier = new RendezvousBarrier("After Commit", 2, BARRIER_TIMEOUT, sLogger);

        Thread thread1 = new Thread(new Runnable() {
            public void run() {
                txMap1.startTransaction();
                try {
                    beforeCommitBarrier.meet();
                    txMap1.put("key1", "value2");
                    txMap1.commitTransaction();
                    afterCommitBarrier.call();
                } catch (InterruptedException e) {
                    logger.log(Level.WARNING, "Thread interrupted", e);
                    afterCommitBarrier.reset();
                    beforeCommitBarrier.reset();
                }
            }
        }, "Thread1");

        txMap1.put("key1", "value1");

        txMap1.startTransaction();
        thread1.start();

        report("value1", (String) txMap1.get("key1"));
        beforeCommitBarrier.call();
        afterCommitBarrier.meet();
        // we have read committed as isolation level, that's why I will see the new value of the other thread now
        report("value2", (String) txMap1.get("key1"));

        // now when I override it it should of course be my value again
        txMap1.put("key1", "value3");
View Full Code Here

Examples of org.apache.commons.transaction.util.RendezvousBarrier

        txMap1.rollbackTransaction();
        assertEquals(txMap1.getTransactionState(), TransactionalMapWrapper.STATUS_NO_TRANSACTION);

        txMap1.startTransaction();
        final TransactionalMapWrapper.TxContext ctx = txMap1.suspendTransaction();
        final RendezvousBarrier afterSuspendBarrier =
            new RendezvousBarrier("After Suspend", 2, BARRIER_TIMEOUT, sLogger);

        new Thread(new Runnable() {
            public void run() {
                txMap1.resumeTransaction(ctx);
                txMap1.put("key2", "value2");
                txMap1.suspendTransaction();
                afterSuspendBarrier.call();
            }
        }).start();

        afterSuspendBarrier.meet();
        txMap1.resumeTransaction(ctx);

        assertEquals(txMap1.size(), 1);
        txMap1.put("key3", "value3");
        assertEquals(txMap1.size(), 2);
View Full Code Here

Examples of org.apache.commons.transaction.util.RendezvousBarrier

        final Map map1 = new HashMap();

        final OptimisticMapWrapper txMap1 = (OptimisticMapWrapper) getNewWrapper(map1);

        final RendezvousBarrier beforeCommitBarrier =
            new RendezvousBarrier("Before Commit", 2, BARRIER_TIMEOUT, sLogger);

        final RendezvousBarrier afterCommitBarrier = new RendezvousBarrier("After Commit", 2, BARRIER_TIMEOUT, sLogger);

        Thread thread1 = new Thread(new Runnable() {
            public void run() {
                txMap1.startTransaction();
                try {
                    beforeCommitBarrier.meet();
                    txMap1.put("key1", "value2");
                    txMap1.commitTransaction();
                    afterCommitBarrier.call();
                } catch (InterruptedException e) {
                    logger.log(Level.WARNING, "Thread interrupted", e);
                    afterCommitBarrier.reset();
                    beforeCommitBarrier.reset();
                }
            }
        }, "Thread1");

        txMap1.put("key1", "value1");

        txMap1.startTransaction();
        thread1.start();

        report("value1", (String) txMap1.get("key1"));
        beforeCommitBarrier.call();
        afterCommitBarrier.meet();
        // we have serializable as isolation level, that's why I will still see the old value
        report("value1", (String) txMap1.get("key1"));

        // now when I override it it should of course be my value
        txMap1.put("key1", "value3");
View Full Code Here

Examples of org.apache.commons.transaction.util.RendezvousBarrier

    final Map map1 = new HashMap();

    final OptimisticMapWrapper txMap1 = (OptimisticMapWrapper) getNewWrapper(map1);

    final RendezvousBarrier beforeCommitBarrier =
      new RendezvousBarrier("Before Commit", 2, BARRIER_TIMEOUT, sLogger);

    final RendezvousBarrier afterCommitBarrier = new RendezvousBarrier("After Commit", 2, BARRIER_TIMEOUT, sLogger);

    Thread thread1 = new Thread(new Runnable() {
      public void run() {
        txMap1.startTransaction();
        try {
          beforeCommitBarrier.meet();
          txMap1.put("key1", "value2");
          txMap1.commitTransaction();
          afterCommitBarrier.call();
        } catch (InterruptedException e) {
          logger.log(Level.WARNING, "Thread interrupted", e);
          afterCommitBarrier.reset();
          beforeCommitBarrier.reset();
        }
      }
    }, "Thread1");

    txMap1.put("key1", "value1");

    txMap1.startTransaction();
    thread1.start();

    report("value1", (String) txMap1.get("key1"));
    beforeCommitBarrier.call();
    afterCommitBarrier.meet();
    // we have serializable as isolation level, that's why I will still see the old value
    report("value1", (String) txMap1.get("key1"));

    // now when I override it it should of course be my value
    txMap1.put("key1", "value3");
View Full Code Here

Examples of org.apache.commons.transaction.util.RendezvousBarrier

        final Map map1 = new HashMap();

        final PessimisticMapWrapper txMap1 = (PessimisticMapWrapper) getNewWrapper(map1);

        final RendezvousBarrier beforeCommitBarrier =
            new RendezvousBarrier("Before Commit", 2, BARRIER_TIMEOUT, sLogger);

        final RendezvousBarrier afterCommitBarrier = new RendezvousBarrier("After Commit", 2, BARRIER_TIMEOUT, sLogger);

        Thread thread1 = new Thread(new Runnable() {
            public void run() {
                txMap1.startTransaction();
                txMap1.put("key1", "value2");
View Full Code Here

Examples of org.apache.commons.transaction.util.RendezvousBarrier

        final String res2 = "res2";

        // a read / write lock
        final ReadWriteLockManager manager = new ReadWriteLockManager(sLogger, TIMEOUT);
       
        final RendezvousBarrier restart = new RendezvousBarrier("restart",
                TIMEOUT, sLogger);

        for (int i = 0; i < CONCURRENT_TESTS; i++) {

           System.out.print(".");
           
            final RendezvousBarrier deadlockBarrier1 = new RendezvousBarrier("deadlock1" + i,
                    TIMEOUT, sLogger);

            Thread deadlock = new Thread(new Runnable() {
                public void run() {
                    try {
                        // first both threads get a lock, this one on res2
                        manager.writeLock(owner2, res2);
                        synchronized (deadlockBarrier1) {
                            deadlockBarrier1.meet();
                            deadlockBarrier1.reset();
                        }
                        // if I am first, the other thread will be dead, i.e.
                        // exactly one
                        manager.writeLock(owner2, res1);
                    } catch (LockException le) {
                        assertEquals(le.getCode(), LockException.CODE_DEADLOCK_VICTIM);
                        deadlockCnt++;
                    } catch (InterruptedException ie) {
                    } finally {
                        manager.releaseAll(owner2);
                        try {
                            synchronized (restart) {
                                restart.meet();
                                restart.reset();
                            }
                            } catch (InterruptedException ie) {}
                    }
                }
            }, "Deadlock Thread");

            deadlock.start();

            try {
                // first both threads get a lock, this one on res2
                manager.readLock(owner1, res1);
                synchronized (deadlockBarrier1) {
                    deadlockBarrier1.meet();
                    deadlockBarrier1.reset();
                }
                //          if I am first, the other thread will be dead, i.e. exactly
                // one
                manager.readLock(owner1, res2);
            } catch (LockException le) {
View Full Code Here

Examples of org.apache.commons.transaction.util.RendezvousBarrier

        // a read / write lock
        final ReadWriteLockManager manager = new ReadWriteLockManager(sLogger,
                TIMEOUT);

        final RendezvousBarrier restart = new RendezvousBarrier("restart", 5, TIMEOUT, sLogger);

        final TurnBarrier cb = new TurnBarrier("cb1", TIMEOUT, sLogger, 1);

        for (int i = 0; i < CONCURRENT_TESTS; i++) {
           
            System.out.print(".");

            // thread that accesses lock of res1 just to cause interference and
            // possibly detect concurrency problems
            Thread jamThread1 = new Thread(new Runnable() {
                public void run() {
                    try {
                        for (int i = 0; i < 10; i++) {
                            manager.readLock(jamowner1, res1);
                            Thread.sleep(10);
                            manager.releaseAll(jamowner1);
                            Thread.sleep(10);
                            manager.writeLock(jamowner1, res1);
                            Thread.sleep(10);
                            manager.releaseAll(jamowner1);
                            Thread.sleep(10);
                        }
                    } catch (LockException le) {
                        fail("Jam Thread should not fail");
                    } catch (InterruptedException ie) {
                    } finally {
                        manager.releaseAll(jamowner1);
                        synchronized (restart) {
                            try {
                                synchronized (restart) {
                                    restart.meet();
                                    restart.reset();
                                }
                                } catch (InterruptedException ie) {}
                        }
                    }
                }
            }, "Jam Thread #1");

            jamThread1.start();

            // thread that accesses lock of res1 just to cause interference and
            // possibly detect concurrency problems
            Thread jamThread2 = new Thread(new Runnable() {
                public void run() {
                    try {
                        for (int i = 0; i < 10; i++) {
                            manager.writeLock(jamowner2, res1);
                            Thread.sleep(10);
                            manager.releaseAll(jamowner2);
                            Thread.sleep(10);
                            manager.readLock(jamowner2, res1);
                            Thread.sleep(10);
                            manager.releaseAll(jamowner2);
                            Thread.sleep(10);
                        }
                    } catch (LockException le) {
                        fail("Jam Thread should not fail");
                    } catch (InterruptedException ie) {
                    } finally {
                        manager.releaseAll(jamowner2);
                        synchronized (restart) {
                            try {
                                synchronized (restart) {
                                    restart.meet();
                                    restart.reset();
                                }
                                } catch (InterruptedException ie) {}
                        }
                    }
                }
            }, "Jam Thread #2");

            jamThread2.start();

            Thread t1 = new Thread(new Runnable() {
                public void run() {
                    try {
                        cb.waitForTurn(2);
                        manager.readLock(owner2, res2);
                        cb.signalTurn(3);
                        cb.waitForTurn(5);
                        synchronized (manager.getLock(res1)) {
                            cb.signalTurn(6);
                            manager.writeLock(owner2, res1);
                        }
                    } catch (LockException le) {
                        assertEquals(le.getCode(), LockException.CODE_DEADLOCK_VICTIM);
                        deadlockCnt++;
                    } catch (InterruptedException ie) {
                    } finally {
                        manager.releaseAll(owner2);
                        synchronized (restart) {
                            try {
                                synchronized (restart) {
                                    restart.meet();
                                    restart.reset();
                                }
                                } catch (InterruptedException ie) {}
                        }
                    }
                }
            }, "Thread #1");

            t1.start();

            Thread t2 = new Thread(new Runnable() {
                public void run() {
                    try {
                        cb.waitForTurn(3);
                        manager.readLock(owner3, res3);
                        synchronized (manager.getLock(res2)) {
                            cb.signalTurn(5);
                            manager.writeLock(owner3, res2);
                        }
                    } catch (LockException le) {
                        assertEquals(le.getCode(), LockException.CODE_DEADLOCK_VICTIM);
                        deadlockCnt++;
                    } catch (InterruptedException ie) {
                    } finally {
                        manager.releaseAll(owner3);
                        synchronized (restart) {
                            try {
                                synchronized (restart) {
                                    restart.meet();
                                    restart.reset();
                                }
                                } catch (InterruptedException ie) {}
                        }
                    }
                }
            }, "Thread #2");

            t2.start();

            try {
                cb.waitForTurn(1);
                manager.readLock(owner1, res1);
                cb.signalTurn(2);
                cb.waitForTurn(6);
                manager.writeLock(owner1, res3);
            } catch (LockException le) {
                assertEquals(le.getCode(), LockException.CODE_DEADLOCK_VICTIM);
                deadlockCnt++;
            } catch (InterruptedException ie) {
            } finally {
                manager.releaseAll(owner1);
                synchronized (restart) {
                    try {
                        synchronized (restart) {
                            restart.meet();
                            restart.reset();
                        }
                    } catch (InterruptedException ie) {
                    }
                }
            }
View Full Code Here

Examples of org.apache.commons.transaction.util.RendezvousBarrier

        // a read / write lock
        final ReadWriteUpgradeLockManager manager = new ReadWriteUpgradeLockManager(sLogger,
                TIMEOUT);

        final RendezvousBarrier restart = new RendezvousBarrier("restart", 3, TIMEOUT, sLogger);

        final TurnBarrier cb = new TurnBarrier("cb1", TIMEOUT, sLogger, 1);

        for (int i = 0; i < CONCURRENT_TESTS; i++) {
           
            System.out.print(".");

            Thread t1 = new Thread(new Runnable() {
                public void run() {
                    try {
                        cb.waitForTurn(2);
                        manager.upgradeLock(owner2, res1);
                        cb.signalTurn(3);
                        cb.waitForTurn(5);
                        synchronized (manager.getLock(res1)) {
                            cb.signalTurn(6);
                            manager.writeLock(owner2, res1);
                        }
                        // we must always be first as we will be preferred over
                        // as I had the upgrade
                        // lock before
                        synchronized (this) {
                            if (first == null)
                                first = owner2;
                        }
                        manager.releaseAll(owner2);
                        synchronized (restart) {
                            restart.meet();
                            restart.reset();
                        }
                    } catch (InterruptedException ie) {
                    }
                }
            }, "Thread #1");

            t1.start();

            Thread t2 = new Thread(new Runnable() {
                public void run() {
                    try {
                        // I wait until the others are blocked
                        // when I release my single read lock, thread #1 always
                        // should be the
                        // next to get the lock as it is preferred over the main
                        // thread
                        // that only waits for a read lock
                        cb.waitForTurn(6);
                        synchronized (manager.getLock(res1)) {
                            cb.signalTurn(7);
                            manager.readLock(owner3, res1);
                        }
                        synchronized (this) {
                            if (first == null)
                                first = owner3;
                        }
                        manager.releaseAll(owner3);
                        synchronized (restart) {
                            restart.meet();
                            restart.reset();
                        }
                    } catch (InterruptedException ie) {
                    }
                }
            }, "Thread #2");

            t2.start();

            cb.waitForTurn(1);
            manager.readLock(owner1, res1);
            cb.signalTurn(2);
            cb.waitForTurn(3);
            manager.release(owner1, res1);
            manager.readLock(owner1, res1);
            cb.signalTurn(5);
            cb.waitForTurn(7);
            synchronized (manager.getLock(res1)) {
                manager.releaseAll(owner1);
            }
            synchronized (restart) {
                restart.meet();
                restart.reset();
            }

            assertEquals(first, owner2);
            first = null;
            cb.reset();
View Full Code Here

Examples of org.apache.commons.transaction.util.RendezvousBarrier

        final String res1 = "res1";

        final ReadWriteLock lock = new ReadWriteLock(res1, sLogger);

        final RendezvousBarrier restart = new RendezvousBarrier("restart", 3, TIMEOUT, sLogger);

        final TurnBarrier cb = new TurnBarrier("cb1", TIMEOUT, sLogger, 1);

        for (int i = 0; i < CONCURRENT_TESTS; i++) {
           
            System.out.print(".");

            Thread t1 = new Thread(new Runnable() {
                public void run() {
                    try {
                        cb.waitForTurn(2);
                        synchronized (lock) {
                            cb.signalTurn(3);
                            lock.acquire(owner2, ReadWriteLock.WRITE_LOCK, true,
                                    GenericLock.COMPATIBILITY_REENTRANT, true, TIMEOUT);
                        }
                        lock.release(owner2);
                        synchronized (restart) {
                            restart.meet();
                            restart.reset();
                        }
                    } catch (InterruptedException ie) {
                    }
                }
            }, "Thread #1");

            t1.start();

            Thread t2 = new Thread(new Runnable() {
                public void run() {
                    try {
                        cb.waitForTurn(3);
                        synchronized (lock) {
                            cb.signalTurn(4);
                            lock.acquire(owner3, ReadWriteLock.WRITE_LOCK, true,
                                    GenericLock.COMPATIBILITY_REENTRANT, true, TIMEOUT);
                        }
                        lock.release(owner3);
                        synchronized (restart) {
                            restart.meet();
                            restart.reset();
                        }
                    } catch (InterruptedException ie) {
                    }
                }
            }, "Thread #2");

            t2.start();

            cb.waitForTurn(1);
            lock.acquireRead(owner1, TIMEOUT);
            cb.signalTurn(2);
            cb.waitForTurn(4);
            synchronized (lock) {
                lock.release(owner1);
            }
            synchronized (restart) {
                restart.meet();
                restart.reset();
            }

            cb.reset();
        }
View Full Code Here

Examples of org.apache.commons.transaction.util.RendezvousBarrier

        final String res1 = "res1";

        final GenericLockManager manager = new GenericLockManager(1, sLogger, TIMEOUT, -1);

        final RendezvousBarrier restart = new RendezvousBarrier("restart", 2, TIMEOUT, sLogger);

        final TurnBarrier cb = new TurnBarrier("cb1", TIMEOUT, sLogger, 1);

        for (int i = 0; i < CONCURRENT_TESTS; i++) {
           
            System.out.print(".");

            Thread t1 = new Thread(new Runnable() {
                public void run() {
                    try {
                        cb.waitForTurn(2);
                        manager.lock(owner2, res1, 1, true);
                        cb.signalTurn(3);
                        manager.releaseAll(owner2);
                        synchronized (restart) {
                            restart.meet();
                            restart.reset();
                        }
                    } catch (InterruptedException ie) {
                    }
                }
            }, "Thread #1");

            t1.start();

            cb.waitForTurn(1);
            manager.startGlobalTimeout(owner1, 500);
            manager.lock(owner1, res1, 1, true);
            cb.signalTurn(2);
            cb.waitForTurn(3);
            boolean failed = false;
            try {
                manager.tryLock(owner1, res1, 1, true);
            } catch (LockException le) {
                failed = true;
            }
            assertTrue(failed);
            manager.releaseAll(owner1);
            failed = false;
            try {
                manager.tryLock(owner1, res1, 1, true);
            } catch (LockException le) {
                failed = true;
            }
            assertFalse(failed);
            manager.releaseAll(owner1);
            synchronized (restart) {
                restart.meet();
                restart.reset();
            }

            cb.reset();
        }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.