Examples of RendezvousBarrier


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

        final ReadWriteUpgradeLockManager manager = new ReadWriteUpgradeLockManager(sLogger, 100);

        int concurrentThreads = 7;
        int threads = CONCURRENT_TESTS * concurrentThreads;
       
        final RendezvousBarrier end = new RendezvousBarrier("end", threads + 1, TIMEOUT, sLogger);
       
        sLogger.logInfo("\n\nStarting "+threads+" threads\n\n");

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

            final int cnt = i;
           
            System.out.print(".");

            Thread t1 = new Thread(new Runnable() {
                public void run() {
                    try {
                        manager.readLock(owner1, res1);
                        manager.readLock(owner1, res2);
                        manager.upgradeLock(owner1, res3);
                        manager.writeLock(owner1, res3);
                    } catch (LockException ie) {
                        System.out.print("-");
                    } finally {
                        manager.releaseAll(owner1);
                        end.call();
                    }
                }
            }, "Thread #1");

            Thread t2 = new Thread(new Runnable() {
                public void run() {
                    try {
                        manager.readLock(owner2, res1);
                        manager.readLock(owner2, res2);
                        manager.upgradeLock(owner2, res3);
                        manager.writeLock(owner2, res3);
                    } catch (LockException ie) {
                        System.out.print("-");
                    } finally {
                        manager.releaseAll(owner2);
                        end.call();
                    }
                }
            }, "Thread #2");

            Thread t3 = new Thread(new Runnable() {
                public void run() {
                    try {
                        manager.startGlobalTimeout(owner3, 10 + cnt);
                        manager.readLock(owner3, res1);
                        manager.readLock(owner3, res2);
                        manager.upgradeLock(owner3, res3);
                        manager.writeLock(owner3, res3);
                    } catch (LockException le) {
                        if (le.getCode() == LockException.CODE_TIMED_OUT) {
                            System.out.print("*");
                        } else {
                            System.out.print("-");
                        }
                    } finally {
                        manager.releaseAll(owner3);
                        end.call();
                    }
                }
            }, "Thread #3");

            Thread t4 = new Thread(new Runnable() {
                public void run() {
                    try {
                        manager.readLock(owner4, res1);
                        manager.readLock(owner4, res2);
                        manager.upgradeLock(owner4, res3);
                        manager.writeLock(owner4, res3);
                    } catch (LockException le) {
                        System.out.print("-");
                    } finally {
                        manager.releaseAll(owner4);
                        end.call();
                    }
                }
            }, "Thread #4");

                Thread deadlock1 = new Thread(new Runnable() {
                public void run() {
                    try {
                        manager.writeLock(owner5, res2);
                        manager.writeLock(owner5, res1);
                    } catch (LockException le) {
                        assertEquals(le.getCode(), LockException.CODE_DEADLOCK_VICTIM);
                        System.out.print("-");
                    } finally {
                        manager.releaseAll(owner5);
                        end.call();
                    }
                }
            }, "Deadlock1 Thread");

            Thread deadlock2 = new Thread(new Runnable() {
                public void run() {
                    try {
                        manager.readLock(owner6, res1);
                        manager.readLock(owner6, res2);
                    } catch (LockException le) {
                        assertEquals(le.getCode(), LockException.CODE_DEADLOCK_VICTIM);
                        System.out.print("-");
                    } finally {
                        manager.releaseAll(owner6);
                        end.call();
                    }
                }
            }, "Deadlock1 Thread");

            Thread reader = new Thread(new Runnable() {
                public void run() {
                    try {
                        manager.readLock("reader", res1);
                        manager.readLock("reader", res2);
                        manager.readLock("reader", res3);
                    } catch (LockException ie) {
                        System.out.print("-");
                    } finally {
                        manager.releaseAll("reader");
                        end.call();
                    }
                }
            }, "Reader Thread");


            t4.start();
            t3.start();
            reader.start();
            t1.start();
            deadlock2.start();
            t2.start();
            deadlock1.start();
        }
        // wait until all threads have really terminated
        end.meet();

    }
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 restart = new RendezvousBarrier("restart",
                TIMEOUT, sLogger);

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

            final RendezvousBarrier deadlockBarrier1 = new RendezvousBarrier("deadlock" + i,
                    TIMEOUT, sLogger);

            Thread thread1 = new Thread(new Runnable() {
                public void run() {
                    txMap1.startTransaction();
                    try {
                        // first both threads get a lock, this one on res2
                        txMap1.put("key2", "value2");
                        synchronized (deadlockBarrier1) {
                            deadlockBarrier1.meet();
                            deadlockBarrier1.reset();
                        }
                        // if I am first, the other thread will be dead, i.e.
                        // exactly one
                        txMap1.put("key1", "value2");
                        txMap1.commitTransaction();
                    } catch (LockException le) {
                        assertEquals(le.getCode(), LockException.CODE_DEADLOCK_VICTIM);
                        deadlockCnt++;
                        txMap1.rollbackTransaction();
                    } catch (InterruptedException ie) {
                    } finally {
                        try {
                        synchronized (restart) {
                            restart.meet();
                            restart.reset();
                        }
                        } catch (InterruptedException ie) {}

                    }
                }
            }, "Thread1");

            thread1.start();

            txMap1.startTransaction();
            try {
                // first both threads get a lock, this one on res2
                txMap1.get("key1");
                synchronized (deadlockBarrier1) {
                    deadlockBarrier1.meet();
                    deadlockBarrier1.reset();
                }
                //          if I am first, the other thread will be dead, i.e. exactly
                // one
                txMap1.get("key2");
                txMap1.commitTransaction();
View Full Code Here

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) {
                    sLogger.logWarning("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(), Status.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
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.