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

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


            drm.registerListener("HASingleton", listener);
            drm.add("HASingleton", "HASingleton");           
         }
        
         // Create a semaphore to gate the threads and acquire all its permits
         Semaphore semaphore = new Semaphore(count + 1);
         for (int i = 0; i <= count; i++)
            semaphore.acquire();
        
         DeployerThread[] deployers = new DeployerThread[keys.length];
         for (int i = 0; i < count; i++)
         {
            DistributedReplicantManager.ReplicantListener listener = null;
            if (conflicting)
            {
               listener = new MockHASingletonDeployer(deployer, keys[i], log);
            }
            else
            {
               listener = new NullListener();
            }
            deployers[i] = new DeployerThread(deployer, keys[i], listener, semaphore, log);
            deployers[i].start();
         }
        
         String[] jgKeys = keys;
         if (!conflicting)
         {
            // The JGroups thread also deals with the MockHASingletonDeployer
            // key that the DeployerThreads don't
            jgKeys = new String[keys.length + 1];
            System.arraycopy(keys, 0, jgKeys, 0, keys.length);
            jgKeys[keys.length] = "HASingleton";           
         }
         JGroupsThread jgThread = new JGroupsThread(drm, jgKeys, remote.getName(), semaphore);
         jgThread.start();
        
         // Launch the threads
         semaphore.release(count + 1);
        
         boolean reacquired = false;
         try
         {
            // Give the threads 5 secs to acquire the semaphore
            long maxElapsed = System.currentTimeMillis() + 5000;
            for (int i = 0; i < keys.length; i++)
            {
               if (deployers[i].getCount() < 0)
               {
                  assertTrue("Thread " + keys[i] + " started in time",
                              maxElapsed - System.currentTimeMillis() > 0);
                  sleepThread(10);
                  i--; // try again
               }  
            }
           
            while (jgThread.getCount() < 0)
            {
               assertTrue("jgThread started in time",
                           maxElapsed - System.currentTimeMillis() > 0);
               sleepThread(10);              
            }
            // Reaquire all the permits, thus showing the threads didn't deadlock
           
            // Give them 500 ms per loop
            maxElapsed = System.currentTimeMillis() + (500 * LOOP_COUNT);
            for (int i = 0; i <= count; i++)
            {
               long waitTime = maxElapsed - System.currentTimeMillis();
               assertTrue("Acquired thread " + i, semaphore.attempt(waitTime));
            }
           
            reacquired = true;
           
            // Ensure there were no exceptions
View Full Code Here


      return true;
   }  

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

      //Make sure everyone is in sync
      blockUntilViewsReceived(receivers, 60000);

      // Sleep to ensure the threads get all the semaphore tickets
View Full Code Here

      ArrayList channels = new ArrayList(count);     
      try
      {
         // Create a semaphore and take all its permits
         Semaphore semaphore = new Semaphore(count);
         takeAllPermits(semaphore, count);

         // Create channels and their threads that will block on the semaphore       
         for (int i = 0; i < count; i++)
         {
            FlushTestReceiver channel = null;
            if(isMuxChannelUsed())
            {
               channel = new FlushTestReceiver(names[i],muxFactory[i%muxFactoryCount], semaphore, useTransfer);              
            }
            else
            {
               channel = new FlushTestReceiver(names[i], semaphore, useTransfer);              
            }
            channels.add(channel);

            // Release one ticket at a time to allow the thread to start working                          
            channel.start();                     
            if (!useTransfer)
            {
               semaphore.release(1);
            }
            sleepThread(2000);
         }

        
         if(isMuxChannelUsed())
         {
            blockUntilViewsReceived(channels,muxFactoryCount, 60000);
         }
         else
         {
            blockUntilViewsReceived(channels, 60000);
         }
       

         //if state transfer is used release all at once
         //clear all channels of view events
         if (useTransfer)
         { 
            for (Iterator iter = channels.iterator(); iter.hasNext();)
            {
               FlushTestReceiver app = (FlushTestReceiver) iter.next();
               app.clear();
              
            }           
            semaphore.release(count);
         }

         // Sleep to ensure the threads get all the semaphore tickets
         sleepThread(1000);
View Full Code Here

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

         // Create activation threads that will block on the semaphore       
         for (int i = 0; i < count; i++)
         {
            if(largeState)
            {
               if(isMuxChannelUsed())
               {
                  channels[i] = new ConcurrentStartupChannelWithLargeState(names[i],muxFactory[i%getMuxFactoryCount()],semaphore);
               }
               else
               {
                  channels[i] = new ConcurrentStartupChannelWithLargeState(semaphore, names[i],useDispatcher);
               }
            }           
            else
            {
            
               if(isMuxChannelUsed())
               {
                  channels[i] = new ConcurrentStartupChannel(names[i],muxFactory[i%getMuxFactoryCount()],semaphore);
               }
               else
               {
                  channels[i] = new ConcurrentStartupChannel(names[i],semaphore,useDispatcher);
               }
            }

            // Release one ticket at a time to allow the thread to start working                          
            channels[i].start();
            semaphore.release(1);
            sleepRandom(1500);
         }

         // Make sure everyone is in sync
         if(isMuxChannelUsed())
View Full Code Here

     
      int count = names.length;
      ConcurrentStateTransfer[] channels = new ConcurrentStateTransfer[count];
     
      //Create a semaphore and take all its tickets
      Semaphore semaphore = new Semaphore(count);
      takeAllPermits(semaphore, count);
     
      try
      {                 

         // Create activation threads that will block on the semaphore       
         for (int i = 0; i < count; i++)
         {
            if(largeState)
            {
               if(isMuxChannelUsed())
               {
                  channels[i] = new ConcurrentLargeStateTransfer(names[i],muxFactory[i%getMuxFactoryCount()],semaphore);
               }
               else
               {
                  channels[i] = new ConcurrentLargeStateTransfer(names[i],semaphore,useDispatcher);
               }
            }
            else
            {   
               if(isMuxChannelUsed())
               {
                  channels[i] = new ConcurrentStateTransfer(names[i],muxFactory[i%getMuxFactoryCount()],semaphore);
               }
               else
               {
                  channels[i] = new ConcurrentStateTransfer(names[i],semaphore,useDispatcher);
               }
            }

            // Start threads and let them join the channel                          
            channels[i].start();
            sleepThread(2000);
         }

         // Make sure everyone is in sync
         if(isMuxChannelUsed())
         {
            blockUntilViewsReceived(channels,getMuxFactoryCount(), 60000);
         }
         else
         {
            blockUntilViewsReceived(channels, 60000);
         }

         sleepThread(2000);
         //Unleash hell !
         semaphore.release(count);

         // Sleep to ensure the threads get all the semaphore tickets
         sleepThread(2000);

         //Reacquire the semaphore tickets; when we have them all
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

            drm.registerListener("HASingleton", listener);
            drm.add("HASingleton", "HASingleton");           
         }
        
         // Create a semaphore to gate the threads and acquire all its permits
         Semaphore semaphore = new Semaphore(count + 1);
         for (int i = 0; i <= count; i++)
            semaphore.acquire();
        
         DeployerThread[] deployers = new DeployerThread[keys.length];
         for (int i = 0; i < count; i++)
         {
            DistributedReplicantManager.ReplicantListener listener = null;
            if (conflicting)
            {
               listener = new MockHASingletonDeployer(deployer, keys[i], log);
            }
            else
            {
               listener = new NullListener();
            }
            deployers[i] = new DeployerThread(deployer, keys[i], listener, semaphore, log);
            deployers[i].start();
         }
        
         String[] jgKeys = keys;
         if (!conflicting)
         {
            // The JGroups thread also deals with the MockHASingletonDeployer
            // key that the DeployerThreads don't
            jgKeys = new String[keys.length + 1];
            System.arraycopy(keys, 0, jgKeys, 0, keys.length);
            jgKeys[keys.length] = "HASingleton";           
         }
         JGroupsThread jgThread = new JGroupsThread(drm, jgKeys, remote.getName(), semaphore);
         jgThread.start();
        
         // Launch the threads
         semaphore.release(count + 1);
        
         boolean reacquired = false;
         try
         {
            // Give the threads 5 secs to acquire the semaphore
            long maxElapsed = System.currentTimeMillis() + 5000;
            for (int i = 0; i < keys.length; i++)
            {
               if (deployers[i].getCount() < 0)
               {
                  assertTrue("Thread " + keys[i] + " started in time",
                              maxElapsed - System.currentTimeMillis() > 0);
                  sleepThread(10);
                  i--; // try again
               }  
            }
           
            while (jgThread.getCount() < 0)
            {
               assertTrue("jgThread started in time",
                           maxElapsed - System.currentTimeMillis() > 0);
               sleepThread(10);              
            }
            // Reaquire all the permits, thus showing the threads didn't deadlock
           
            // Give them 500 ms per loop
            maxElapsed = System.currentTimeMillis() + (500 * LOOP_COUNT);
            for (int i = 0; i <= count; i++)
            {
               long waitTime = maxElapsed - System.currentTimeMillis();
               assertTrue("Acquired thread " + i, semaphore.attempt(waitTime));
            }
           
            reacquired = true;
           
            // Ensure there were no exceptions
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

         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

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.