Package EDU.oswego.cs.dl.util.concurrent

Examples of EDU.oswego.cs.dl.util.concurrent.Semaphore


//      int CACHE_COUNT = names.length;
      CacheUser[] cacheUsers = new CacheUser[CACHE_COUNT];     
     
      try {
         // Create a semaphore and take all its tickets
         Semaphore semaphore = new Semaphore(CACHE_COUNT);        
         for (int i = 0; i < CACHE_COUNT; i++) {
            semaphore.acquire();
         }  
        
         // Create activation threads that will block on the semaphore
         TreeCache[] caches = new TreeCache[CACHE_COUNT];
         for (int i = 0; i < CACHE_COUNT; i++) {
            cacheUsers[i] = new CacheUser(semaphore, String.valueOf(i), sync);
            caches[i] = cacheUsers[i].getTreeCache();
         }
        
         // Make sure everyone is in sync
         TestingUtil.blockUntilViewsReceived(caches, 60000);
        
         // Release the semaphore to allow the threads to start work
         semaphore.release(CACHE_COUNT);
        
         // Sleep to ensure the threads get all the semaphore tickets
         TestingUtil.sleepThread(1000);
        
         // Reacquire the semaphore tickets; when we have them all
         // we know the threads are done        
         for (int i = 0; i < CACHE_COUNT; i++) {
            boolean acquired = semaphore.attempt(60000);
            if (!acquired)
               fail("failed to acquire semaphore " + i);
         }

         // Sleep to allow any async calls to clear
View Full Code Here


      CacheActivator[] activators = new CacheActivator[count];
     
     
      try {
         // Create a semaphore and take all its tickets
         Semaphore semaphore = new Semaphore(count);        
         for (int i = 0; i < count; i++) {
            semaphore.acquire();
         }  
        
         // Create activation threads that will block on the semaphore
         TreeCacheMBean[] caches = new TreeCacheMBean[count];
         for (int i = 0; i < count; i++) {
            activators[i] = new CacheActivator(semaphore, names[i], sync);
            caches[i] = activators[i].getTreeCache();
            activators[i].start();
         }
        
         // Make sure everyone is in sync
         TestingUtil.blockUntilViewsReceived(caches, 60000);
        
         // Release the semaphore to allow the threads to start work
         semaphore.release(count);
        
         // Sleep to ensure the threads get all the semaphore tickets
          TestingUtil.sleepThread((long)1000);

          // Reacquire the semaphore tickets; when we have them all
         // we know the threads are done        
         for (int i = 0; i < count; i++) {
            boolean acquired = semaphore.attempt(60000);
            if (!acquired)
               fail("failed to acquire semaphore " + i);
         }

         // Sleep to allow any async calls to clear
View Full Code Here

        
         TreeCacheMBean[] caches = new TreeCacheMBean[count + 1];
         caches[0] = cacheA;
        
         // Create a semaphore and take all its tickets
         Semaphore semaphore = new Semaphore(count);        
         for (int i = 0; i < count; i++) {
            semaphore.acquire();
         }  
        
         // Create stressor threads that will block on the semaphore
                 
         for (int i = 0; i < count; i++)
         {
            stressors[i] = new CacheStressor(semaphore, names[i], sync);
            caches[i + 1] = stressors[i].getTreeCache();
            stressors[i].start();
         }
        
         // Make sure everyone's views are in sync
         TestingUtil.blockUntilViewsReceived(caches, 60000);
        
         // Repeat the basic test four times
         //for (int x = 0; x < 4; x++)
         for (int x = 0; x < 1; x++)
         {
            if (x > 0)
            {
               // Reset things by inactivating the region
               // and enabling the stressors
               for (int i = 0; i < count; i++)
               {
                  cacheA.inactivateRegion("/" + names[i]);
                  System.out.println("Run " + x + "-- /" + names[i] + " inactivated on A");
                  stressors[i].startPuts();
               }
            }
           
            // Release the semaphore to allow the threads to start work
            semaphore.release(count);
           
            // Sleep to ensure the threads get all the semaphore tickets
            // and to ensure puts are actively in progress
             TestingUtil.sleepThread((long)300);

             // Activate cacheA
            for (int i = 0; i < count; i++)
            {
//              System.out.println("Activating /" + names[i] + " on A");
               cacheA.activateRegion("/" + names[i]);
               // Stop the stressor so we don't pollute cacheA's state
               // with too many messages sent after activation -- we want
               // to compare transferred state with the sender
               stressors[i].stopPuts();
               System.out.println("Run " + x + "-- /" + names[i] + " activated on A");
               // Reacquire one semaphore ticket
               boolean acquired = semaphore.attempt(60000);
               if (!acquired)
                  fail("failed to acquire semaphore " + i);
              
               // Pause to allow other work to proceed
                TestingUtil.sleepThread((long)100);
View Full Code Here

         if (pool == null)
         {
            pool = new LinkedList();
            connectionPools.put(address, pool);
            log.debug("Creating semaphore with size " + maxPoolSize);
            semaphore = new Semaphore(maxPoolSize);
            semaphores.put(address, semaphore);
           
            if (trace)
            {
               synchronized (pool)
View Full Code Here

        final int PRODUCER_COUNT=Integer.parseInt(System.getProperty("PRODUCER_COUNT","10"));
        final int CONSUMER_COUNT=Integer.parseInt(System.getProperty("CONSUMER_COUNT","10"));
       
        final ProgressPrinter pp = new ProgressPrinter(MESSAGE_COUNT*2, 5);

        final Semaphore connectionsEstablished = new Semaphore(1-(CONSUMER_COUNT+PRODUCER_COUNT));
        final Latch startTest = new Latch();
        final Semaphore testsFinished = new Semaphore(1-(CONSUMER_COUNT+PRODUCER_COUNT));
       
        final Callable producer = new Callable() {
            public Object call() throws JMSException, InterruptedException {
                Connection connection = connectionFactory.createConnection();
                Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                MessageProducer producer = session.createProducer(dest);
                producer.setDeliveryMode(DeliveryMode.PERSISTENT);
                BytesMessage message = session.createBytesMessage();
                message.writeBytes(new byte[1024]);
                connection.start();
               
                connectionsEstablished.release();
               
                startTest.acquire();               
                final int msgs = (MESSAGE_COUNT/PRODUCER_COUNT)+1;
                for (int i = 0; i < msgs; i++) {
                    pp.increment();
                    producer.send(message);
                }               
               
                testsFinished.release();               
                connection.close();
                return null;
            }
        };
       
        final Callable consumer = new Callable() {
            public Object call() throws JMSException, InterruptedException {
                final Latch doneLatch = new Latch();               
                Connection connection = connectionFactory.createConnection();               
                Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                MessageConsumer consumer = session.createConsumer(dest);

                connectionsEstablished.release();
                startTest.acquire();

                final int msgs = (MESSAGE_COUNT/CONSUMER_COUNT)-1;
                consumer.setMessageListener(new MessageListener(){
                    int counter=0;
                    public void onMessage(Message msg) {               
                        pp.increment();
                        counter++;
                        if( counter >= msgs ) {
                            doneLatch.release();
                        }
                    }
                });
                connection.start();
                doneLatch.acquire();

                testsFinished.release();               
                connection.close();
                return null;
            }
        };
       
        final Throwable workerError[] = new Throwable[1];
        for( int i=0; i < PRODUCER_COUNT; i++ ) {
            new Thread("Producer:"+i) {
                public void run() {
                    try {
                        producer.call();
                    } catch (Throwable e) {
                        e.printStackTrace();
                        workerError[0] = e;
                    }
                }
            }.start();
        }

        for( int i=0; i < CONSUMER_COUNT; i++ ) {
            new Thread("Consumer:"+i) {
                public void run() {
                    try {
                        consumer.call();
                    } catch (Throwable e) {
                        workerError[0] = e;
                    }
                }
            }.start();
        }

        connectionsEstablished.acquire();

//        System.out.println("ready.");
//        System.in.read();System.in.read();
       
        startTest.release();
        long start = System.currentTimeMillis();
        testsFinished.acquire();
        long end = System.currentTimeMillis();
        System.out.println(getName() + ": test duration: " + (end - start) + " ms, published+acked msg/s: "
                + (MESSAGE_COUNT * 1000f / (end - start)));
       
//        System.out.println("ready.");
View Full Code Here

         if (pool == null)
         {
            pool = new LinkedList();
            connectionPools.put(address, pool);
            log.debug("Creating semaphore with size " + maxPoolSize);
            semaphore = new Semaphore(maxPoolSize);
            semaphores.put(address, semaphore);
           
            if (trace)
            {
               synchronized (pool)
View Full Code Here

         // Bisocket callback client invoker doesn't share socket pools because of the danger
         // that two distinct callback servers could have the same "artifical" port.
         pool = new LinkedList();
         log.debug("Creating semaphore with size " + maxPoolSize);
         semaphore = new Semaphore(maxPoolSize);
         return;
      }

      // Client on client side.
      super.handleConnect();
View Full Code Here

         // Bisocket callback client invoker doesn't share socket pools because of the danger
         // that two distinct callback servers could have the same "artifical" port.
         pool = new LinkedList();
         log.debug("Creating semaphore with size " + maxPoolSize);
         semaphore = new Semaphore(maxPoolSize);
         return;
      }

      // Client on client side.
      super.handleConnect();
View Full Code Here

         if (pool == null)
         {
            pool = new LinkedList();
            connectionPools.put(address, pool);
            log.debug("Creating semaphore with size " + maxPoolSize);
            semaphore = new Semaphore(maxPoolSize);
            semaphores.put(address, semaphore);
           
            if (trace)
            {
               synchronized (pool)
View Full Code Here

         // Bisocket callback client invoker doesn't share socket pools because of the danger
         // that two distinct callback servers could have the same "artifical" port.
         pool = new LinkedList();
         log.debug("Creating semaphore with size " + maxPoolSize);
         semaphore = new Semaphore(maxPoolSize);
         return;
      }

      // Client on client side.
      super.handleConnect();
View Full Code Here

TOP

Related Classes of EDU.oswego.cs.dl.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.