Package java.util.concurrent

Examples of java.util.concurrent.Semaphore


        final int count=names.length;

        final ConcurrentStartupChannel[] channels=new ConcurrentStartupChannel[count];
        try {
            // Create a semaphore and take all its permits
            Semaphore semaphore=new Semaphore(count);
            semaphore.acquire(count);

            // Create activation threads that will block on the semaphore
            for(int i=0;i < count;i++) {
                if(i == 0)
                    channels[i]=new ConcurrentStartupChannel(names[i], semaphore);
                else
                    channels[i]=new ConcurrentStartupChannel((JChannel)channels[0].getChannel(), names[i], semaphore);
                channels[i].start();
                semaphore.release(1);
                if(i == 0)
                    Util.sleep(1500); // sleep after the first node to educe the chances of a merge
            }

            // Make sure everyone is in sync
            Channel[] tmp=new Channel[channels.length];
            for(int i=0; i < channels.length; i++)
                tmp[i]=channels[i].getChannel();

            Util.blockUntilViewsReceived(30000, 500, tmp);
            System.out.println(">>>> all nodes have the same view <<<<");

            // Re-acquire the semaphore tickets; when we have them all we know the threads are done
            boolean acquired=semaphore.tryAcquire(count, 20, TimeUnit.SECONDS);
            if(!acquired) {
                log.warn("Most likely a bug, analyse the stack below:");
                log.warn(Util.dumpThreads());
            }
View Full Code Here


@Test(groups = Global.FLUSH, sequential = false)
public class FlushTest extends ChannelTestBase {

    @Test
    public void testSingleChannel() throws Exception {
        Semaphore s = new Semaphore(1);
        FlushTestReceiver receivers[] = new FlushTestReceiver[] { new FlushTestReceiver("c1", s, 0,
                        FlushTestReceiver.CONNECT_ONLY) };
        receivers[0].start();
        s.release(1);

        // Make sure everyone is in sync
        Channel[] tmp = new Channel[receivers.length];
        for (int i = 0; i < receivers.length; i++)
            tmp[i] = receivers[i].getChannel();
        Util.blockUntilViewsReceived(60000, 1000, tmp);

        // Reacquire the semaphore tickets; when we have them all
        // we know the threads are done
        s.tryAcquire(1, 60, TimeUnit.SECONDS);
        receivers[0].cleanup();
        Util.sleep(1000);

        checkEventStateTransferSequence(receivers[0]);
    }
View Full Code Here

        int count = names.length;

        List<FlushTestReceiver> channels = new ArrayList<FlushTestReceiver>(count);
        try {
            // Create a semaphore and take all its permits
            Semaphore semaphore = new Semaphore(count);
            semaphore.acquire(count);

            // Create channels and their threads that will block on the
            // semaphore
            boolean first = true;
            for (String channelName : names) {
                FlushTestReceiver channel = null;
                if (first)
                    channel = new FlushTestReceiver(channelName, semaphore, 0, connectType);
                else {
                    channel = new FlushTestReceiver((JChannel) channels.get(0).getChannel(),
                                    channelName, semaphore, 0, connectType);
                }
                channels.add(channel);

                // Release one ticket at a time to allow the thread to start working
                channel.start();
                semaphore.release(1);
                if (first)
                    Util.sleep(3000); // minimize changes of a merge happening
                first = false;
            }

            Channel[] tmp = new Channel[channels.size()];
            int cnt = 0;
            for (FlushTestReceiver receiver : channels)
                tmp[cnt++] = receiver.getChannel();
            Util.blockUntilViewsReceived(30000, 1000, tmp);

            // Reacquire the semaphore tickets; when we have them all
            // we know the threads are done
            semaphore.tryAcquire(count, 40, TimeUnit.SECONDS);

            Util.sleep(1000); //let all events propagate...
            for (FlushTestReceiver app : channels)
                app.getChannel().setReceiver(null);
            for (FlushTestReceiver app : channels)
View Full Code Here

    private final ExecutorService executor;
    private final Semaphore semaphore;

    public BoundedWorkQueueExecutor(ExecutorService exec, int bound) {
        this.executor = exec;
        this.semaphore = new Semaphore(bound);
    }
View Full Code Here

    }

    public BoundedWorkQueueExecutor(int threadPoolSize, int workQueueSize, String threadName) {
        this.executor = ExecutorFactory.newFixedThreadPool(threadPoolSize, threadName);
        int bound = workQueueSize + threadPoolSize;
        this.semaphore = new Semaphore(bound);
    }
View Full Code Here

    * </ol>
    */
   public void testReadUncommitted() throws Throwable
   {
      final LockStrategy s = new LockStrategyReadUncommitted();
      final Semaphore sem = new MyFIFOSemaphore(1);
      final CyclicBarrier barrier = new CyclicBarrier(2);

      Thread t1 = new Thread("t1")
      {
         Lock lock = null;

         public void run()
         {
            try
            {
               sem.acquire(); // we're first to the semaphore

               // log("waiting on barrier");
               barrier.await(); // wait until t2 joins us
               // log("passed barrier");
               lock = s.readLock();
               lock.tryLock(TIMEOUT, TimeUnit.MILLISECONDS);
               log("1st read: value is " + value);
               assertEquals(10, value);
               sem.release(); // give t2 time to make the modification
               TestingUtil.sleepThread(100);

               sem.acquire(); // to read the uncommitted modification by t2
               log("2nd read: value is " + value + "; we should see t2's uncommitted change (20)");
               assertEquals(20, value); // we're seeing the modification by t2 before t2 committed (a.k.a. released the lock)
               sem.release();
               TestingUtil.sleepThread(100);

               sem.acquire(); // to read the committed change by t2
               log("3rd read: value is still " + value + "; we should see t2's committed change");
               assertEquals(20, value);
            }
            catch (Throwable ex)
            {
               t1_ex = ex;
            }
            finally
            {
               if (lock != null)
                  lock.unlock();
               sem.release();
            }
         }
      };


      Thread t2 = new Thread("t2")
      {
         Lock lock = null;

         public void run()
         {
            try
            {
               TestingUtil.sleepThread(100);
               barrier.await();
               sem.acquire();
               lock = s.writeLock();
               lock.tryLock(TIMEOUT, TimeUnit.MILLISECONDS);
               log("changing value from " + value + " to 20");
               value = 20;
               sem.release(); // now t1 can read the uncommitted modification
               TestingUtil.sleepThread(100);

               sem.acquire(); // to unlock the lock
               log("committing the TX");
               lock.unlock();
            }
            catch (Throwable ex)
            {
               t2_ex = ex;
            }
            finally
            {
               if (lock != null)
                  lock.unlock();
               sem.release();
            }
         }
      };

      t1.start();
View Full Code Here

    public void testStateTransferWhileSending() throws Exception {

        StateTransferApplication[] apps=new StateTransferApplication[APP_COUNT];
        try {
            Semaphore semaphore=new Semaphore(APP_COUNT);
            semaphore.acquire(APP_COUNT);

            int from=0, to=MSG_SEND_COUNT;
            for(int i=0;i < apps.length;i++) {
                if(i == 0)
                    apps[i]=new StateTransferApplication(semaphore, names[i], from, to);
                else
                    apps[i]=new StateTransferApplication((JChannel)apps[0].getChannel(), semaphore, names[i], from, to);
                from+=MSG_SEND_COUNT;
                to+=MSG_SEND_COUNT;
            }

            for(int i=0;i < apps.length;i++) {
                StateTransferApplication app=apps[i];
                app.start();
                semaphore.release();
                //avoid merge
                Util.sleep(3000);
            }

            // Make sure everyone is in sync
            Channel[] tmp=new Channel[apps.length];
            for(int i=0; i < apps.length; i++)
                tmp[i]=apps[i].getChannel();

            Util.blockUntilViewsReceived(60000, 1000, tmp);

            // Reacquire the semaphore tickets; when we have them all
            // we know the threads are done
            boolean acquired=semaphore.tryAcquire(apps.length, 30, TimeUnit.SECONDS);
            if(!acquired) {
                log.warn("Most likely a bug, analyse the stack below:");
                log.warn(Util.dumpThreads());
            }
View Full Code Here

    protected final Map<RequestContext, Thread> _runningThreads = new ConcurrentIdentityHashMap<RequestContext, Thread>(12);
    protected final ExecutorService _executors = ExecutorFactory.newCachedThreadPool(60L, "ParallelQP");

    public QueryProcessor(ResponseListener handler) {
        super(handler);
        this._throttle = new Semaphore(ThrottedRemoteSequenceProxy.NUM_THROTTLE);
    }
View Full Code Here

        this._throttle = throttle;
    }

    public ThrottedRemoteSequenceProxy(Sequence<Item> delegate) {
        super(delegate);
        this._throttle = new Semaphore(NUM_THROTTLE);
    }
View Full Code Here

        this._throttle = new Semaphore(NUM_THROTTLE);
    }

    @Override
    public void run() {
        final Semaphore throttle = _throttle;
        throttle.acquireUninterruptibly();
        try {
            compute();
        } finally {
            throttle.release();
        }
    }
View Full Code Here

TOP

Related Classes of java.util.concurrent.Semaphore

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.