Examples of Semaphore


Examples of java.util.concurrent.Semaphore

    this.server = server;
    this.serverSocket = serverSocket;
    // reserve a few places for graceful disconnects with informative
    // messages
    int countOfConnectionPermits = server.getMaxConnections() + 10;
    this.connectionPermits = new Semaphore(countOfConnectionPermits);
    this.sessionThreads = new HashSet<Session>(countOfConnectionPermits * 4 / 3 + 1);
  }
View Full Code Here

Examples of java.util.concurrent.Semaphore

      this.jmcp = jmcp;
      this.log = log;
      this.subPoolContext = subPoolContext;
      this.trace = log.isTraceEnabled();
      this.cls = new ArrayList(this.maxSize);
      this.permits = new Semaphore(this.maxSize, true);
 
      if (poolParams.prefill)
      {
         PoolFiller.fillPool(this);
      }
View Full Code Here

Examples of java.util.concurrent.Semaphore

      throws Exception
   {
      int numThreads = 8;
      int numLoops = 100;
     
      Semaphore semaphore = new Semaphore(numThreads);
     
      ThreadGroup runnerGroup = new ThreadGroup("TestRunners");
     
      ClassLoader ours = Thread.currentThread().getContextClassLoader();
     
      // The classloader we want channel threads to use
      ClassLoaderLeakHandler handler = new ClassLoaderLeakHandler(testLoader, semaphore, runnerGroup);
     
      MessageDispatcher[] dispatchers = new MessageDispatcher[2];
     
//      Thread.currentThread().setContextClassLoader(testLoader);   
//      try
//      {
         channel1 = (Channel) factoryMeth.invoke(factory1, factory1Args);
         dispatchers[0] = new MessageDispatcher(channel1, handler, handler, handler);
        
         channel1.connect("leaktest");
         assertEquals("No classloader leak on channel1 connect", null, handler.getLeakedClassLoader());
        
         channel2 = (Channel) factoryMeth.invoke(factory2, factory2Args);
         dispatchers[1] = new MessageDispatcher(channel2, handler, handler, handler);
        
         channel2.connect("leaktest");
         assertEquals("No classloader leak on channel2 connect", null, handler.getLeakedClassLoader());
//      }
//      finally
//      {
//         Thread.currentThread().setContextClassLoader(ours);
//      }
     
      log.info("Channels connected");
     
      ClassLoaderLeakRunner[] runners = new ClassLoaderLeakRunner[numThreads];     
     
      for (int i = 0; i < runners.length; i++)
      {
         MessageDispatcher disp = dispatchers[i % 2];
           
         runners[i] = new ClassLoaderLeakRunner(disp, numLoops, runnerGroup, semaphore);        
      }

      semaphore.acquire(numThreads);
     
      for (int i = 0; i < runners.length; i++)
      {
         runners[i].start();
      }
     
      semaphore.release(numThreads);
     
      try
      {
         assertTrue("messages received within 15 seconds", semaphore.tryAcquire(numThreads, 15, TimeUnit.SECONDS));
        
         log.info("Messages received");
      }
      finally
      {
View Full Code Here

Examples of java.util.concurrent.Semaphore

            int newSeeds = -1;
            //if (seeds.length > 1) {
            // holding a reference to all started threads
            int contactedSeedCount = 0;
            final List<Thread> syncList = Collections.synchronizedList(new LinkedList<Thread>()); // memory for threads
            final Semaphore sync = new Semaphore(attempts);

            // going through the peer list and starting a new publisher thread for each peer
            int i = 0;
            while (si.hasNext()) {
                seed = si.next();
                if (seed == null) {
                    sync.acquire();
                    continue;
                }
                i++;

                final String address = seed.getClusterAddress();
                if (log.isFine()) log.logFine("HELLO #" + i + " to peer '" + seed.get(yacySeed.NAME, "") + "' at " + address); // debug
                final String seederror = seed.isProper(false);
                if ((address == null) || (seederror != null)) {
                    // we don't like that address, delete it
                    sb.peers.peerActions.peerDeparture(seed, "peer ping to peer resulted in address = " + address + "; seederror = " + seederror);
                    sync.acquire();
                } else {
                    // starting a new publisher thread
                    contactedSeedCount++;
                    (new publishThread(yacyCore.publishThreadGroup, seed, sync, syncList)).start();
                }
            }

            // receiving the result of all started publisher threads
            for (int j = 0; j < contactedSeedCount; j++) {

                // waiting for the next thread to finish
                sync.acquire();

                // if this is true something is wrong ...
                if (syncList.isEmpty()) {
                    log.logWarning("PeerPing: syncList.isEmpty()==true");
                    continue;
View Full Code Here

Examples of java.util.concurrent.Semaphore

        Semaphore trigger;

        public SecondarySearchSuperviser() {
            this.abstractsCache = new TreeMap<String, SortedMap<String, StringBuilder>>();
            this.checkedPeers = new TreeSet<String>();
            this.trigger = new Semaphore(0);
        }
View Full Code Here

Examples of java.util.concurrent.Semaphore

        public void run() {
            // transform the reference container into a stream of parsed entries
            final BlockingQueue<WordReferenceVars> vars = WordReferenceVars.transform(this.container);

            // start the transformation threads
            final Semaphore termination = new Semaphore(this.threads);
            final NormalizeWorker[] worker = new NormalizeWorker[this.threads];
            for (int i = 0; i < this.threads; i++) {
                worker[i] = new NormalizeWorker(this.out, termination);
                worker[i].start();
            }
View Full Code Here

Examples of java.util.concurrent.Semaphore

    //private ReferenceFactory<ReferenceType> factory;
    private   boolean                      terminate;
    private final int                          writeBufferSize;

    public IODispatcher(final int dumpQueueLength, final int mergeQueueLength, final int writeBufferSize) {
        this.termination = new Semaphore(0);
        this.controlQueue = new Semaphore(0);
        this.dumpQueue = new ArrayBlockingQueue<DumpJob<? extends Reference>>(dumpQueueLength);
        this.mergeQueue = new ArrayBlockingQueue<MergeJob>(mergeQueueLength);
        this.writeBufferSize = writeBufferSize;
        this.terminate = false;
    }
View Full Code Here

Examples of java.util.concurrent.Semaphore

        @Override
      public void run() {
          // start the transformation threads
          final int cores0 = Math.min(cores, this.container.size() / 100) + 1;
          final Semaphore termination = new Semaphore(cores0);
          final TransformWorker[] worker = new TransformWorker[cores0];
          for (int i = 0; i < cores0; i++) {
            worker[i] = new TransformWorker(this.out, termination);
            worker[i].start();
          }
View Full Code Here

Examples of java.util.concurrent.Semaphore

        tmp.renameTo(targetFile);
    }

    public Response load(final Request request, final CacheStrategy cacheStrategy, final long maxFileSize, final boolean checkBlacklist) throws IOException {
        final String url = request.url().toNormalform(true, false);
        Semaphore check = this.loaderSteering.get(url);
        if (check != null) {
            // a loading process may be going on for that url
            try { check.tryAcquire(5, TimeUnit.SECONDS);} catch (final InterruptedException e) {}
            // now the process may have terminated and we run a normal loading
            // which may be successful faster because of a cache hit
        }

        this.loaderSteering.put(url, new Semaphore(0));
        try {
            final Response response = loadInternal(request, cacheStrategy, maxFileSize, checkBlacklist);
            check = this.loaderSteering.remove(url);
            if (check != null) check.release(1000);
            return response;
        } catch (final IOException e) {
            // release the semaphore anyway
            check = this.loaderSteering.remove(url);
            if (check != null) check.release(1000);
            //Log.logException(e);
            throw new IOException(e);
        }
    }
View Full Code Here

Examples of java.util.concurrent.Semaphore

    public WeakPriorityBlockingQueue(final int maxsize) {
        // the maxsize is the maximum number of entries in the stack
        // if this is set to -1, the size is unlimited
        this.queue = new TreeSet<Element<E>>();
        this.drained = new ArrayList<Element<E>>();
        this.enqueued = new Semaphore(0);
        this.maxsize = maxsize;
    }
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.