Examples of AtomicBoolean


Examples of java.util.concurrent.atomic.AtomicBoolean

   public void testThreadLockedByThread() throws InterruptedException
   {
      InvocationContextContainer icc = getInvocationContextContainer();
      final OwnableReentrantLock lock = new OwnableReentrantLock(icc);
      final AtomicBoolean acquired = new AtomicBoolean(false);
      final AtomicBoolean threwExceptionOnRelease = new AtomicBoolean(false);


      lock.lock();
      assert lock.getOwner().equals(Thread.currentThread());
      assert lock.getHoldCount() == 1;

      Thread t = new Thread()
      {
         public void run()
         {
            try
            {
               acquired.set(lock.tryLock(10, TimeUnit.MILLISECONDS));
            }
            catch (InterruptedException e)
            {
               // do nothing
            }

            try
            {
               lock.unlock();
            }
            catch (IllegalMonitorStateException e)
            {
               // expected
               threwExceptionOnRelease.set(true);
            }
         }
      };

      t.start();
      t.join();

      assert !acquired.get() : "Second thread should not have acquired lock";
      assert threwExceptionOnRelease.get() : "Second thread should have thrown an exception trying to release lock";

      lock.unlock();
      assert !lock.isLocked();
   }
View Full Code Here

Examples of java.util.concurrent.atomic.AtomicBoolean

      InvocationContextContainer icc = getInvocationContextContainer();
      final OwnableReentrantLock lock = new OwnableReentrantLock(icc);
      GlobalTransaction gtx = new GlobalTransaction();
      gtx.setId(10);
      icc.get().setGlobalTransaction(gtx);
      final AtomicBoolean acquired = new AtomicBoolean(false);
      final AtomicBoolean threwExceptionOnRelease = new AtomicBoolean(false);

      lock.lock();
      assert lock.getOwner().equals(gtx);
      assert lock.getHoldCount() == 1;

      Thread t = new Thread()
      {
         public void run()
         {
            try
            {
               acquired.set(lock.tryLock(10, TimeUnit.MILLISECONDS));
            }
            catch (InterruptedException e)
            {
               // do nothing
            }

            try
            {
               lock.unlock();
            }
            catch (IllegalMonitorStateException e)
            {
               // expected
               threwExceptionOnRelease.set(true);
            }
         }
      };

      t.start();
      t.join();

      assert !acquired.get() : "Second thread should not have acquired lock";
      assert threwExceptionOnRelease.get() : "Second thread should have thrown an exception trying to release lock";

      lock.unlock();
      assert !lock.isLocked();
   }
View Full Code Here

Examples of java.util.concurrent.atomic.AtomicBoolean

   public void testGtxLockedByThread() throws InterruptedException
   {
      final InvocationContextContainer icc = getInvocationContextContainer();
      final OwnableReentrantLock lock = new OwnableReentrantLock(icc);
      final AtomicBoolean acquired = new AtomicBoolean(false);
      final AtomicBoolean threwExceptionOnRelease = new AtomicBoolean(false);

      lock.lock();
      assert lock.getOwner().equals(Thread.currentThread());
      assert lock.getHoldCount() == 1;

      Thread t = new Thread()
      {
         public void run()
         {
            try
            {
               GlobalTransaction gtx = new GlobalTransaction();
               gtx.setId(10);
               icc.get().setGlobalTransaction(gtx);
               acquired.set(lock.tryLock(10, TimeUnit.MILLISECONDS));
            }
            catch (InterruptedException e)
            {
               // do nothing
            }

            try
            {
               lock.unlock();
            }
            catch (IllegalMonitorStateException e)
            {
               // expected
               threwExceptionOnRelease.set(true);
            }
         }
      };

      t.start();
      t.join();

      assert !acquired.get() : "Second thread should not have acquired lock";
      assert threwExceptionOnRelease.get() : "Second thread should have thrown an exception trying to release lock";

      lock.unlock();
      assert !lock.isLocked();
   }
View Full Code Here

Examples of java.util.concurrent.atomic.AtomicBoolean

   public void testGtxLockedByGtxFail() throws InterruptedException
   {
      final InvocationContextContainer icc = getInvocationContextContainer();
      final OwnableReentrantLock lock = new OwnableReentrantLock(icc);
      final AtomicBoolean acquired = new AtomicBoolean(false);
      final AtomicBoolean threwExceptionOnRelease = new AtomicBoolean(false);
      GlobalTransaction gtx = new GlobalTransaction();
      gtx.setId(10);
      icc.get().setGlobalTransaction(gtx);

      lock.lock();
      assert lock.getOwner().equals(gtx);
      assert lock.getHoldCount() == 1;

      Thread t = new Thread()
      {
         public void run()
         {
            try
            {
               GlobalTransaction gtx = new GlobalTransaction();
               gtx.setId(20);
               icc.get().setGlobalTransaction(gtx);
               acquired.set(lock.tryLock(10, TimeUnit.MILLISECONDS));
            }
            catch (InterruptedException e)
            {
               // do nothing
            }

            try
            {
               lock.unlock();
            }
            catch (IllegalMonitorStateException e)
            {
               // expected
               threwExceptionOnRelease.set(true);
            }
         }
      };

      t.start();
      t.join();

      assert !acquired.get() : "Second thread should not have acquired lock";
      assert threwExceptionOnRelease.get() : "Second thread should have thrown an exception trying to release lock";

      lock.unlock();
      assert !lock.isLocked();
   }
View Full Code Here

Examples of java.util.concurrent.atomic.AtomicBoolean

   public void testGtxLockedByGtxSuccess() throws InterruptedException
   {
      final InvocationContextContainer icc = getInvocationContextContainer();
      final OwnableReentrantLock lock = new OwnableReentrantLock(icc);
      final AtomicBoolean acquired = new AtomicBoolean(false);
      GlobalTransaction gtx = new GlobalTransaction();
      gtx.setId(10);
      icc.get().setGlobalTransaction(gtx);

      lock.lock();
      assert lock.getOwner().equals(gtx);
      assert lock.getHoldCount() == 1;

      Thread t = new Thread()
      {
         public void run()
         {
            try
            {
               GlobalTransaction gtx = new GlobalTransaction();
               gtx.setId(10);
               icc.get().setGlobalTransaction(gtx);
               acquired.set(lock.tryLock(10, TimeUnit.MILLISECONDS));
            }
            catch (InterruptedException e)
            {
               // do nothing
            }
         }
      };

      t.start();
      t.join();

      assert acquired.get() : "Second thread should have acquired lock";
      assert lock.getHoldCount() == 2;
      lock.unlock();
      lock.unlock();
      assert !lock.isLocked();
   }
View Full Code Here

Examples of java.util.concurrent.atomic.AtomicBoolean

    }

    // @see java.lang.Runnable#run()
    @Override
    public void run() {
        final AtomicBoolean shutdown = new AtomicBoolean();
        // This thread is only spawned so that we can differentiate between
        // an interrupt of a task and an interrupt causing a shutdown of
        // runner itself.
        Thread executionThread = new Thread() {

            // @see java.lang.Thread#run()
            @Override
            public void run() {
                Runnable runnable = null;
                // This task exits by being interrupted when the task isn't running
                while (!shutdown.get()) {
                    runnable = (Runnable)ch.downcall(new ExecutorEvent(
                        ExecutorEvent.CONSUMER_READY, null));
                    if (Thread.interrupted()) {
                        if (runnable != null) {
                            // We assume that if an interrupt occurs here that
                            // it is trying to close down the task.  Since the
                            // window is so small.  Therefore if we get a
                            // task we need to reject it so it can be passed
                            // off to a different consumer
                            ch.down(new ExecutorEvent(ExecutorEvent.TASK_COMPLETE,
                                new Object[]{runnable, new InterruptedException()}));
                        }
                        continue;
                    }
                    Throwable throwable = null;
                    try {
                        runnable.run();
                    }
                    // This can only happen if user is directly doing an execute(Runnable)
                    catch (Throwable t) {
                        _logger.error("Unexpected Runtime Error encountered in Runnable request", t);
                        throwable = t;
                    }
                    ch.down(new ExecutorEvent(ExecutorEvent.TASK_COMPLETE,
                        throwable != null ? new Object[]{runnable, throwable} : runnable));
                }
            }
        };

        executionThread.setName(Thread.currentThread().getName() + "- Task Runner");
        executionThread.start();
       
        try {
            executionThread.join();
        }
        catch (InterruptedException e) {
            shutdown.set(true);
            executionThread.interrupt();
            if (_logger.isTraceEnabled()) {
                _logger.trace("Shutting down Execution Runner");
            }
        }
View Full Code Here

Examples of java.util.concurrent.atomic.AtomicBoolean

        Statement stat = conn.createStatement();
        final Statement stat2 = conn2.createStatement();
        stat2.execute("set lock_timeout 1000");
        stat.execute("create table test(id int primary key, name varchar)");
        conn.setAutoCommit(false);
        final AtomicBoolean committed = new AtomicBoolean(false);
        Task t = new Task() {
            public void call() throws SQLException {
                try {
//System.out.println("insert2 hallo");
                    stat2.execute("insert into test values(0, 'Hallo')");
//System.out.println("insert2 hallo done");
                } catch (SQLException e) {
//System.out.println("insert2 hallo e " + e);
                    if (!committed.get()) {
                        throw e;
                    }
                }
            }
        };
//System.out.println("insert hello");
        stat.execute("insert into test values(0, 'Hello')");
        t.execute();
        Thread.sleep(500);
//System.out.println("insert hello commit");
        committed.set(true);
        conn.commit();
        t.get();
        ResultSet rs;
        rs = stat.executeQuery("select name from test");
        rs.next();
View Full Code Here

Examples of java.util.concurrent.atomic.AtomicBoolean

        visitAll();
        return Collections.singleton(new DefaultConfigurableFileTree(tmpDir, null, null));
    }

    public FileTree visit(FileVisitor visitor) {
        AtomicBoolean stopFlag = new AtomicBoolean();
        Visit visit = new Visit(visitor, stopFlag);
        for (Map.Entry<RelativePath, Closure> entry : elements.entrySet()) {
            if (stopFlag.get()) {
                break;
            }
            RelativePath path = entry.getKey();
            Closure generator = entry.getValue();
            visit.visit(path, generator);
View Full Code Here

Examples of java.util.concurrent.atomic.AtomicBoolean

   public void testGetAsString()
   {
      Converter converter = new AtomicBooleanConverter();
      assertEquals("", converter.getAsString(null, null, null));
      assertEquals("", converter.getAsString(null, null, ""));
      assertEquals("true", converter.getAsString(null, null, new AtomicBoolean(true)));
      assertEquals("false", converter.getAsString(null, null, new AtomicBoolean(false)));
      try
      {
         converter.getAsString(null, null, new Boolean(true));
         fail();
      }
View Full Code Here

Examples of java.util.concurrent.atomic.AtomicBoolean

    public AtomicBackoffLock(int spinsBeforeYield, int spinsBeforeSleep, long initSleepTime, boolean lock) {
        this._spinsBeforeSleep = spinsBeforeSleep;
        this._spinsBeforeYield = spinsBeforeYield;
        this._initSleepTime = initSleepTime;
        this.state = new AtomicBoolean(lock);
    }
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.