Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*-
   * See the file LICENSE for redistribution information.
   *
   * Copyright (c) 2002, 2013 Oracle and/or its affiliates.  All rights reserved.
   *
   */
  
  package com.sleepycat.je.rep.utilint;
  
 import static com.sleepycat.je.rep.utilint.SizeAwaitMapStatDefinition.N_NO_WAITS;
 import static com.sleepycat.je.rep.utilint.SizeAwaitMapStatDefinition.N_REAL_WAITS;
 import static com.sleepycat.je.rep.utilint.SizeAwaitMapStatDefinition.N_WAIT_TIME;
 
 import java.util.Map;
 import java.util.Set;
 
Creates a Map, that Threads can conveniently wait on to reach a specific size. The wait functionality is provided by the sizeAwait() method defined by this class.
 
 public class SizeAwaitMap<K,V> implements Map<K,V> {
     private EnvironmentImpl envImpl;
 
     /*
      * The latch map. There is a latch for each threshold of interest to a
      * thread.
      */
     private final 
 
     /* The underlying map of interest to threads. */
     private final Map<K,V> map;
 
     private final StatGroup stats;
     private final LongStat nNoWaits;
     private final LongStat nRealWaits;
     private final LongStat nWaitTime;

    
Creates the wrapped Map class. Note that the application must not directly manipulate the underlying map class. The underlying map must be synchronized if it's accessed concurrently.

Parameters:
map the actual map instance.
 
     public SizeAwaitMap(EnvironmentImpl envImplMap<K, V> map) {
         this. = envImpl;
         this. = map;
                               .);
          = new LongStat();
          = new LongStat();
          = new LongStat();
     }
 
     public StatGroup getStatistics() {
         return ;
     }

    
Causes the requesting thread to wait until the map reaches the specified size or the thread is interrupted.

Parameters:
thresholdSize the size to wait for.
Returns:
true if the threshold was reached, false, if the wait timed out.
Throws:
java.lang.InterruptedException for the usual reasons, or if the map was cleared and the size threshold was not actually reached.
 
     public boolean sizeAwait(int thresholdSize,
                              long timeout,
                              TimeUnit unit)
         throws InterruptedException {
 
         assert(thresholdSize >= 0);
         ExceptionAwareCountDownLatch l = null;
         synchronized (this) {
             int size = .size();
             if (thresholdSize  <= size) {
                 .increment();
                 return true;
             }
             l = .get(thresholdSize);
             if (l == null) {
                 l = new ExceptionAwareCountDownLatch(, 1);
                .put(thresholdSizel);
            }
        }
        .increment();
        long startTime = System.currentTimeMillis();
        try {
            return l.awaitOrException(timeoutunit);
        } finally {
            .add((System.currentTimeMillis() - startTime));
        }
    }

    
Used for unit tests only

Returns:
    synchronized int latchCount() {
        return .size();
    }

    
Notes the addition of a new value and counts down any latches that were assigned to that threshold.
    synchronized public V put(K key, V value) {
        V oldValue = .put(keyvalue);
        if (oldValue == null) {
            /* Incremented size */
            CountDownLatch l = .remove(.size());
            if (l != null) {
                l.countDown();
            }
        }
        return oldValue;
    }

    
It's synchronized so that size() has a stable value in the above methods.
    synchronized public V remove(Object key) {
        return .remove(key);
    }
    
    

Deprecated:
Use clear(java.lang.Exception) instead.
    @Override
    public void clear() throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    
Clears the underlying map and the latch map, after first counting them down, thus permitting them to make progress.
    synchronized public void clear(Exception cause) {
        for (ExceptionAwareCountDownLatch l : .values()) {
            l.releaseAwait(cause);
        }
        .clear();
        .clear();
    }
    /* The remaining methods below merely forward to the underlying map. */
    public boolean containsKey(Object key) {
        return .containsKey(key);
    }
    public boolean containsValue(Object value) {
        return .containsKey(value);
    }
    public Set<java.util.Map.Entry<K, V>> entrySet() {
        return .entrySet();
    }
    public V get(Object key) {
        return .get(key);
    }
    public boolean isEmpty() {
        return .isEmpty();
    }
    public Set<K> keySet() {
        return .keySet();
    }
    public void putAll(@SuppressWarnings("unused")
                       Map<? extends K, ? extends V> t) {
        throw EnvironmentFailureException.unexpectedState
            ("putAll not supported");
    }
    public int size() {
        return .size();
    }
    public Collection<V> values() {
        return .values();
    }
New to GrepCode? Check out our FAQ X