Package java.util.concurrent.atomic

Examples of java.util.concurrent.atomic.AtomicBoolean


            catch(Throwable t) {
                log.error("couldn't deliver OOB message " + msg, t);
            }
        }

        final AtomicBoolean processing=win.getProcessing();
        if(!processing.compareAndSet(false, true)) {
            return;
        }

        // try to remove (from the AckReceiverWindow) as many messages as possible and pass them up

        // Prevents concurrent passing up of messages by different threads (http://jira.jboss.com/jira/browse/JGRP-198);
        // this is all the more important once we have a concurrent stack (http://jira.jboss.com/jira/browse/JGRP-181),
        // where lots of threads can come up to this point concurrently, but only 1 is allowed to pass at a time
        // We *can* deliver messages from *different* senders concurrently, e.g. reception of P1, Q1, P2, Q2 can result in
        // delivery of P1, Q1, Q2, P2: FIFO (implemented by UNICAST) says messages need to be delivered only in the
        // order in which they were sent by their senders
        boolean released_processing=false;
        try {
            while(true) {
                List<Message> msgs=win.removeMany(processing, true, max_msg_batch_size); // remove my own messages
                if(msgs == null || msgs.isEmpty()) {
                    released_processing=true;
                    return;
                }

                for(Message m: msgs) {
                    // discard OOB msg: it has already been delivered (http://jira.jboss.com/jira/browse/JGRP-377)
                    if(m.isFlagSet(Message.OOB))
                        continue;
                    try {
                        up_prot.up(new Event(Event.MSG, m));
                    }
                    catch(Throwable t) {
                        log.error("couldn't deliver message " + m, t);
                    }
                }
            }
        }
        finally {
            // processing is always set in win.remove(processing) above and never here ! This code is just a
            // 2nd line of defense should there be an exception before win.remove(processing) sets processing
            if(!released_processing)
                processing.set(false);
        }
    }
View Full Code Here


        private final AtomicBoolean closed;

        public StateInputStream() throws IOException {
            super();
            this.closed = new AtomicBoolean(false);
        }
View Full Code Here

        public StateOutputStream(Address stateRequester, String state_id) throws IOException {
            super();
            this.stateRequester = stateRequester;
            this.state_id = state_id;
            this.closed = new AtomicBoolean(false);
        }
View Full Code Here

     * Tests the initial-delay argument of
     * {@link TimeScheduler#scheduleWithFixedDelay(Runnable, long, long, java.util.concurrent.TimeUnit)}
     */
    @Test(dataProvider="createTimer")
    public void testSchedulerWithFixedDelay(TimeScheduler timer) {
        final AtomicBoolean set=new AtomicBoolean(false);

        Future<?> future=timer.scheduleWithFixedDelay(new Runnable() {
            public void run() {
                set.set(true);
            }
        }, 0, 3000, TimeUnit.MILLISECONDS);

        Util.sleep(500);
        future.cancel(true);

        System.out.println("variable was set: " + set);
        assert set.get();

        future=timer.scheduleWithFixedDelay(new Runnable() {
            public void run() {
                set.set(true);
            }
        }, 300, 3000, TimeUnit.MILLISECONDS);

        Util.sleep(1000);
        future.cancel(true);

        System.out.println("variable was set: " + set);
        assert set.get();
    }
View Full Code Here

     */
    @Test
    public void testLateStart() throws Exception {
        final Lock lock=new ReentrantLock();
        final Condition cond=lock.newCondition();
        AtomicBoolean done=new AtomicBoolean(false);

        System.out.println("-- starting first channel");
        c1=new JChannel(PROPS);
        changeMergeInterval(c1);
        c1.setReceiver(new MyReceiver("c1", done, lock, cond));
        c1.connect("demo");

        System.out.println("-- starting second channel");
        c2=new JChannel(PROPS);
        changeMergeInterval(c2);
        c2.setReceiver(new MyReceiver("c2", done, lock, cond));
        c2.connect("demo");

        System.out.println("-- starting GossipRouter");
        router=new GossipRouter(12001, bind_addr);
        router.start();

        System.out.println("-- waiting for merge to happen --");
        long target_time=System.currentTimeMillis() + 40000;
        lock.lock();
        try {
            while(System.currentTimeMillis() < target_time && done.get() == false) {
                cond.await(1000, TimeUnit.MILLISECONDS);
            }
        }
        finally {
            lock.unlock();
View Full Code Here

            catch(Throwable t) {
                log.error("couldn't deliver OOB message " + msg, t);
            }
        }

        final AtomicBoolean processing=win.getProcessing();
        if(!processing.compareAndSet(false, true)) {
            return;
        }

        // try to remove (from the AckReceiverWindow) as many messages as possible as pass them up

        // Prevents concurrent passing up of messages by different threads (http://jira.jboss.com/jira/browse/JGRP-198);
        // this is all the more important once we have a concurrent stack (http://jira.jboss.com/jira/browse/JGRP-181),
        // where lots of threads can come up to this point concurrently, but only 1 is allowed to pass at a time
        // We *can* deliver messages from *different* senders concurrently, e.g. reception of P1, Q1, P2, Q2 can result in
        // delivery of P1, Q1, Q2, P2: FIFO (implemented by UNICAST) says messages need to be delivered only in the
        // order in which they were sent by their senders
        try {
            while(true) {
                Tuple<List<Message>,Long> tuple=win.removeMany(max_msg_batch_size);
                if(tuple == null)
                    return;
                List<Message> msgs=tuple.getVal1();
                if(msgs.isEmpty())
                    return;

                long highest_removed=tuple.getVal2();
                if(highest_removed > 0)
                    sendAck(sender, highest_removed); // guaranteed not to throw an exception !

                for(Message m: msgs) {
                    // discard OOB msg: it has already been delivered (http://jira.jboss.com/jira/browse/JGRP-377)
                    if(m.isFlagSet(Message.OOB))
                        continue;
                    try {
                        up_prot.up(new Event(Event.MSG, m));
                    }
                    catch(Throwable t) {
                        log.error("couldn't deliver message " + m, t);
                    }
                }
            }
        }
        finally {
            processing.set(false);
        }
    }
View Full Code Here

        // Efficient way of checking whether another thread is already processing messages from 'sender'.
        // If that's the case, we return immediately and let the existing thread process our message
        // (https://jira.jboss.org/jira/browse/JGRP-829). Benefit: fewer threads blocked on the same lock, these threads
        // can be returned to the thread pool
        final AtomicBoolean processing=win.getProcessing();
        if(!processing.compareAndSet(false, true)) {
            return;
        }

        boolean remove_msgs=discard_delivered_msgs && !loopback;
        boolean released_processing=false;
        try {
            while(true) {
                // we're removing a msg and set processing to false (if null) *atomically* (wrt to add())
                List<Message> msgs=win.removeMany(processing, remove_msgs, max_msg_batch_size);
                if(msgs == null || msgs.isEmpty()) {
                    released_processing=true;
                    if(rebroadcasting)
                        checkForRebroadcasts();
                    return;
                }

                for(final Message msg_to_deliver: msgs) {
                    // discard OOB msg if it has already been delivered (http://jira.jboss.com/jira/browse/JGRP-379)
                    if(msg_to_deliver.isFlagSet(Message.OOB) && !msg_to_deliver.setTransientFlagIfAbsent(Message.OOB_DELIVERED))
                        continue;

                    //msg_to_deliver.removeHeader(getName()); // Changed by bela Jan 29 2003: not needed (see above)
                    try {
                        up_prot.up(new Event(Event.MSG, msg_to_deliver));
                    }
                    catch(Throwable t) {
                        log.error("couldn't deliver message " + msg_to_deliver, t);
                    }
                }
            }
        }
        finally {
            // processing is always set in win.remove(processing) above and never here ! This code is just a
            // 2nd line of defense should there be an exception before win.remove(processing) sets processing
            if(!released_processing)
                processing.set(false);
        }
    }
View Full Code Here

      }
   }

   public void testStreamClose() throws Exception
   {
      final AtomicBoolean closed = new AtomicBoolean(false);
      BufferingOutputStream out = new BufferingOutputStream(new ByteArrayOutputStream()
      {
         @Override
         public void close() throws IOException
         {
            closed.set(true);
            super.close();
         }
      }, 1);
      out.close();
      assertTrue(closed.get());
      try
      {
         out.write(0);
      }
      catch (IOException ignore)
View Full Code Here

      // Update and save
      page.setTitle("foo");
      storage_.save(page);

      //
      final AtomicBoolean go = new AtomicBoolean(false);

      // Force a cache update with the entry that will be modified
      // when the main session is closed
    
      new Thread()
      {
         @Override
         public void run()
         {
            try
            {
               begin();
               mgr.openSession();
               storage_.getPage("portal::test::test4");
               session.close();
               end();
            }
            catch (Exception e)
            {
               throw new Error(e);
            }
            finally
            {
               go.set(true);
            }
         }
      }.start();

      //
      while (!go.get())
      {
         Thread.sleep(1);
      }

      // Save the cache should be invalidated
View Full Code Here

   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

TOP

Related Classes of java.util.concurrent.atomic.AtomicBoolean

Copyright © 2018 www.massapicom. 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.