Examples of Semaphore


Examples of java.util.concurrent.Semaphore

        Thread.currentThread().interrupt();
      }
    } else {
      synchronized(queueLock) {
        queue = new LinkedBlockingDeque<Event>(capacity);
        queueRemaining = new Semaphore(capacity);
        queueStored = new Semaphore(0);
      }
    }

    if (bytesRemaining == null) {
      bytesRemaining = new Semaphore(byteCapacity);
      lastByteCapacity = byteCapacity;
    } else {
      if (byteCapacity > lastByteCapacity) {
        bytesRemaining.release(byteCapacity - lastByteCapacity);
        lastByteCapacity = byteCapacity;
View Full Code Here

Examples of java.util.concurrent.Semaphore

    if (getLifecycleState() == LifecycleState.START    // does not support reconfig when running
            || getLifecycleState() == LifecycleState.ERROR)
      stop();

    if (totalStored == null) {
      totalStored = new Semaphore(0);
    }

    if (channelCounter == null) {
      channelCounter = new ChannelCounter(getName());
    }

    // 1) Memory Capacity
    Integer newMemoryCapacity;
    try {
      newMemoryCapacity = context.getInteger(MEMORY_CAPACITY
              , defaultMemoryCapacity);
      if (newMemoryCapacity == null) {
        newMemoryCapacity = defaultMemoryCapacity;
      }
      if (newMemoryCapacity < 0) {
        throw new NumberFormatException(MEMORY_CAPACITY + " must be >= 0");
      }

    } catch(NumberFormatException e) {
      newMemoryCapacity = defaultMemoryCapacity;
      LOGGER.warn("Invalid " + MEMORY_CAPACITY + " specified, initializing " +
          getName() + " channel to default value of {}", defaultMemoryCapacity);
    }
    try {
      resizePrimaryQueue(newMemoryCapacity);
    } catch (InterruptedException e) {
      Thread.currentThread().interrupt();
    }

    // overflowTimeout - wait time before switching to overflow when mem is full
    try {
      Integer newOverflowTimeout =
              context.getInteger(OVERFLOW_TIMEOUT, defaultOverflowTimeout);
      overflowTimeout = (newOverflowTimeout != null) ? newOverflowTimeout
                                                     : defaultOverflowTimeout;
    } catch(NumberFormatException e) {
      LOGGER.warn("Incorrect value for " + getName() + "'s " + OVERFLOW_TIMEOUT
            + " setting. Using default value {}", defaultOverflowTimeout);
      overflowTimeout = defaultOverflowTimeout;
    }

    try {
      Integer newThreshold = context.getInteger(OVERFLOW_DEACTIVATION_THRESHOLD);
      overflowDeactivationThreshold =  (newThreshold != null) ?
                                   newThreshold/100.0
                                 : defaultOverflowDeactivationThreshold / 100.0;
    } catch(NumberFormatException e) {
      LOGGER.warn("Incorrect value for " + getName() + "'s " +
              OVERFLOW_DEACTIVATION_THRESHOLD + ". Using default value {} %",
              defaultOverflowDeactivationThreshold);
      overflowDeactivationThreshold = defaultOverflowDeactivationThreshold / 100.0;
    }

    // 3) Memory consumption control
    try {
      byteCapacityBufferPercentage =
              context.getInteger(BYTE_CAPACITY_BUFFER_PERCENTAGE
                      , defaultByteCapacityBufferPercentage);
    } catch(NumberFormatException e) {
      LOGGER.warn("Error parsing " + BYTE_CAPACITY_BUFFER_PERCENTAGE + " for "
              + getName() + ". Using default="
              + defaultByteCapacityBufferPercentage + ". " + e.getMessage());
      byteCapacityBufferPercentage = defaultByteCapacityBufferPercentage;
    }

    try {
      avgEventSize = context.getInteger(AVG_EVENT_SIZE, defaultAvgEventSize);
    } catch ( NumberFormatException e) {
      LOGGER.warn("Error parsing " + AVG_EVENT_SIZE + " for " + getName()
              + ". Using default = " + defaultAvgEventSize + ". "
              + e.getMessage());
      avgEventSize = defaultAvgEventSize;
    }

    try {
      byteCapacity = (int) ((context.getLong(BYTE_CAPACITY, defaultByteCapacity) * (1 - byteCapacityBufferPercentage * .01 )) / avgEventSize);
      if (byteCapacity < 1) {
        byteCapacity = Integer.MAX_VALUE;
      }
    } catch(NumberFormatException e) {
      LOGGER.warn("Error parsing " + BYTE_CAPACITY + " setting for " + getName()
              + ". Using default = " + defaultByteCapacity + ". "
              + e.getMessage());
      byteCapacity = (int)
            ( (defaultByteCapacity * (1 - byteCapacityBufferPercentage * .01 ))
                    / avgEventSize);
    }


    if (bytesRemaining == null) {
      bytesRemaining = new Semaphore(byteCapacity);
      lastByteCapacity = byteCapacity;
    } else {
      if (byteCapacity > lastByteCapacity) {
        bytesRemaining.release(byteCapacity - lastByteCapacity);
        lastByteCapacity = byteCapacity;
View Full Code Here

Examples of java.util.concurrent.Semaphore

        if (memQueue !=null) {
          newQueue.addAll(memQueue);
        }
        memQueue = newQueue;
        if (memQueRemaining == null) {
          memQueRemaining = new Semaphore(newMemoryCapacity);
        } else {
          int diff = newMemoryCapacity - memoryCapacity;
          memQueRemaining.release(diff);
        }
        memoryCapacity = newMemoryCapacity;
View Full Code Here

Examples of java.util.concurrent.Semaphore

    fsyncInterval = context.getInteger(FileChannelConfiguration
      .FSYNC_INTERVAL, FileChannelConfiguration.DEFAULT_FSYNC_INTERVAL);

    if(queueRemaining == null) {
      queueRemaining = new Semaphore(capacity, true);
    }
    if(log != null) {
      log.setCheckpointInterval(checkpointInterval);
      log.setMaxFileSize(maxFileSize);
    }
View Full Code Here

Examples of java.util.concurrent.Semaphore

      // take a snapshot of the current container state.
      LinkedList<InstanceWrapper> toOutput = new LinkedList<InstanceWrapper>(instances.values());

      Executor executorService = null;
      Semaphore taskLock = null;
      synchronized(lockForExecutorServiceSetter)
      {
         executorService = outputExecutorService;
         if (executorService != null)
            taskLock = new Semaphore(outputConcurrency);
      }

      // This keeps track of the number of concurrently running
      // output tasks so that this method can wait until they're
      // all done to return.
      //
      // It's also used as a condition variable signaling on its
      // own state changes.
      final AtomicLong numExecutingOutputs = new AtomicLong(0);

      // keep going until all of the outputs have been invoked
      while (toOutput.size() > 0 && isRunning)
      {
         for (final Iterator<InstanceWrapper> iter = toOutput.iterator(); iter.hasNext();)
         {
            final InstanceWrapper wrapper = iter.next();
            boolean gotLock = false;

            gotLock = wrapper.tryLock();

            if (gotLock)
            {
               // If we've been evicted then we're on our way out
               // so don't do anything else with this.
               if (wrapper.isEvicted())
               {
                  iter.remove();
                  wrapper.releaseLock();
                  continue;
               }

               final Object instance = wrapper.getInstance(); // only called while holding the lock
               final Semaphore taskSepaphore = taskLock;

               // This task will release the wrapper's lock.
               Runnable task = new Runnable()
               {
                  @Override
                  public void run()
                  {
                     try
                     {
                        if (isRunning && !wrapper.isEvicted())
                           invokeOperation(instance, Operation.output, null);
                     }
                     finally
                     {
                        wrapper.releaseLock();

                        // this signals that we're done.
                        synchronized(numExecutingOutputs)
                        {
                           numExecutingOutputs.decrementAndGet();
                           numExecutingOutputs.notifyAll();
                        }
                        if (taskSepaphore != null) taskSepaphore.release();
                     }
                  }
               };

               synchronized(numExecutingOutputs)
               {
                  numExecutingOutputs.incrementAndGet();
               }

               if (executorService != null)
               {
                  try
                  {
                     taskSepaphore.acquire();
                     executorService.execute(task);
                  }
                  catch (RejectedExecutionException e)
                  {
                     // this may happen because of a race condition between the
                     taskSepaphore.release();
                     wrapper.releaseLock(); // we never got into the run so we need to release the lock
                  }
                  catch (InterruptedException e)
                  {
                     // this can happen while blocked in the semaphore.acquire.
View Full Code Here

Examples of java.util.concurrent.Semaphore

        {
            this.ctr.compareAndSet(idinc, newid);
        }
       
        String strid=String.valueOf(newid);
        Semaphore sem=new Semaphore(0);
       
        DRPCRequest req=new DRPCRequest(args, strid);
        this.idtoStart.put(strid, TimeUtils.current_time_secs());
        this.idtoSem.put(strid, sem);
        ConcurrentLinkedQueue<DRPCRequest> queue=this.acquire_queue(this.requestQueues, function);
        queue.add(req);
        LOG.debug("Waiting for DRPC request for " +function+ " " +args+ " at " +(System.currentTimeMillis()));
        try {
            sem.acquire();
        } catch (InterruptedException e) {
            LOG.error("acquire fail " ,e);
        }
        LOG.debug("Acquired for DRPC request for " +function+ " " +args+ " at " +(System.currentTimeMillis()));
View Full Code Here

Examples of java.util.concurrent.Semaphore

        return String.valueOf(result);
    }

    @Override
    public void result(String id, String result) throws TException {
        Semaphore sem =this.idtoSem.get(id);
        LOG.debug("Received result " +result+ " for id " +id+ " at " +(System.currentTimeMillis()));
        if(sem!=null)
        {
            this.idtoResult.put(id,result);
            sem.release();
        }

    }
View Full Code Here

Examples of java.util.concurrent.Semaphore

        return new DRPCRequest("", "");
    }

    @Override
    public void failRequest(String id) throws TException {
        Semaphore sem =this.idtoSem.get(id);
        LOG.debug("failRequest result  for id " +id+ " at " +(System.currentTimeMillis()));
        if(sem!=null)
        {
            this.idtoResult.put(id,new DRPCExecutionException( "Request failed"));
            sem.release();
        }       
    }
View Full Code Here

Examples of java.util.concurrent.Semaphore

            {
                if(TimeUtils.time_delta(e.getValue())>clearThreadcall.REQUEST_TIMEOUT_SECS)
                {
                    String id=e.getKey();
                    Drpc.this.idtoResult.put(id, new DRPCExecutionException("Request timed out"));
                    Semaphore s=Drpc.this.idtoSem.get(id);
                    if(s!=null)
                    {
                        s.release();
                    }
                    set.add(id);
                }
            }
   
View Full Code Here

Examples of java.util.concurrent.Semaphore

import java.util.concurrent.Semaphore;

public class SemaphoreExtensions {

  public static Object newInstance(Object receiver) {
    return new Semaphore(0, true);
  }
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.