Example usage for java.util.concurrent ConcurrentSkipListSet add

List of usage examples for java.util.concurrent ConcurrentSkipListSet add

Introduction

In this page you can find the example usage for java.util.concurrent ConcurrentSkipListSet add.

Prototype

public boolean add(E e) 

Source Link

Document

Adds the specified element to this set if it is not already present.

Usage

From source file:org.apache.hadoop.hbase.io.encoding.TestPrefixTreeEncoding.java

private static void generateRandomTestData(ConcurrentSkipListSet<KeyValue> kvset, int batchId, boolean useTags,
        PrefixTreeCodec encoder, HFileBlockEncodingContext blkEncodingCtx, DataOutputStream userDataStream)
        throws Exception {
    Random random = new Random();
    for (int i = 0; i < NUM_ROWS_PER_BATCH; ++i) {
        if (random.nextInt(100) < 50)
            continue;
        for (int j = 0; j < NUM_COLS_PER_ROW; ++j) {
            if (random.nextInt(100) < 50)
                continue;
            if (!useTags) {
                KeyValue kv = new KeyValue(getRowKey(batchId, i), CF_BYTES, getQualifier(j),
                        getValue(batchId, i, j));
                kvset.add(kv);
            } else {
                KeyValue kv = new KeyValue(getRowKey(batchId, i), CF_BYTES, getQualifier(j), 0l,
                        getValue(batchId, i, j), new Tag[] { new Tag((byte) 1, "metaValue1") });
                kvset.add(kv);/*from  ww  w .  ja va 2  s  .c  o m*/
            }
        }
    }
    encoder.startBlockEncoding(blkEncodingCtx, userDataStream);
    for (KeyValue kv : kvset) {
        encoder.encode(kv, blkEncodingCtx, userDataStream);
    }
    encoder.endBlockEncoding(blkEncodingCtx, userDataStream, null);
}

From source file:org.apache.hadoop.hbase.procedure2.TestProcedureSchedulerConcurrency.java

private void testConcurrentWaitWake(final boolean useWakeBatch) throws Exception {
    final int WAIT_THRESHOLD = 2500;
    final int NPROCS = 20;
    final int NRUNS = 500;

    final ProcedureScheduler sched = procSched;
    for (long i = 0; i < NPROCS; ++i) {
        sched.addBack(new TestProcedureWithEvent(i));
    }//  w  w  w . ja v a2s.  co  m

    final Thread[] threads = new Thread[4];
    final AtomicInteger waitCount = new AtomicInteger(0);
    final AtomicInteger wakeCount = new AtomicInteger(0);

    final ConcurrentSkipListSet<TestProcedureWithEvent> waitQueue = new ConcurrentSkipListSet<TestProcedureWithEvent>();
    threads[0] = new Thread() {
        @Override
        public void run() {
            long lastUpdate = 0;
            while (true) {
                final int oldWakeCount = wakeCount.get();
                if (useWakeBatch) {
                    ProcedureEvent[] ev = new ProcedureEvent[waitQueue.size()];
                    for (int i = 0; i < ev.length; ++i) {
                        ev[i] = waitQueue.pollFirst().getEvent();
                        LOG.debug("WAKE BATCH " + ev[i] + " total=" + wakeCount.get());
                    }
                    sched.wakeEvents(ev.length, ev);
                    wakeCount.addAndGet(ev.length);
                } else {
                    int size = waitQueue.size();
                    while (size-- > 0) {
                        ProcedureEvent ev = waitQueue.pollFirst().getEvent();
                        sched.wakeEvent(ev);
                        LOG.debug("WAKE " + ev + " total=" + wakeCount.get());
                        wakeCount.incrementAndGet();
                    }
                }
                if (wakeCount.get() != oldWakeCount) {
                    lastUpdate = System.currentTimeMillis();
                } else if (wakeCount.get() >= NRUNS
                        && (System.currentTimeMillis() - lastUpdate) > WAIT_THRESHOLD) {
                    break;
                }
                Threads.sleepWithoutInterrupt(25);
            }
        }
    };

    for (int i = 1; i < threads.length; ++i) {
        threads[i] = new Thread() {
            @Override
            public void run() {
                while (true) {
                    TestProcedureWithEvent proc = (TestProcedureWithEvent) sched.poll();
                    if (proc == null)
                        continue;

                    sched.suspendEvent(proc.getEvent());
                    waitQueue.add(proc);
                    sched.waitEvent(proc.getEvent(), proc);
                    LOG.debug("WAIT " + proc.getEvent());
                    if (waitCount.incrementAndGet() >= NRUNS) {
                        break;
                    }
                }
            }
        };
    }

    for (int i = 0; i < threads.length; ++i) {
        threads[i].start();
    }
    for (int i = 0; i < threads.length; ++i) {
        threads[i].join();
    }

    sched.clear();
}

From source file:org.apache.qpid.server.security.group.FileGroupDatabase.java

private ConcurrentSkipListSet<String> buildUserSetFromCommaSeparateValue(String userString) {
    String[] users = userString.split(",");
    final ConcurrentSkipListSet<String> userSet = new ConcurrentSkipListSet<String>();
    for (String user : users) {
        final String trimmed = user.trim();
        if (!trimmed.isEmpty()) {
            userSet.add(trimmed);
        }//from w  ww . ja  v  a 2s .  com
    }
    return userSet;
}

From source file:org.sindice.siren.index.Utils.java

/**
 * Flatten a list of triples to n-tuples containing many objects for the same
 * predicate. Generate one n-tuple per predicate.
 * /*w  w  w . java2  s .c o  m*/
 * @param values
 *          The list of n-triples.
 * @return The n-tuples concatenated.
 */
private static synchronized void flattenNTriples(final StringBuilder triples,
        final ConcurrentHashMap<String, ConcurrentSkipListSet<String>> map,
        final ConcurrentSkipListSet<String> types, final ConcurrentSkipListSet<String> label,
        final ConcurrentSkipListSet<String> description, final boolean isOut) {
    try {
        initParser();
        parser.parse(new StringReader(triples.toString()), "");
        for (Statement st : collector.getStatements()) {
            sb.setLength(0);
            final String subject = sb.append('<').append(st.getSubject().toString()).append('>').toString();
            sb.setLength(0);
            final String predicate = sb.append('<').append(st.getPredicate().toString()).append('>').toString();
            sb.setLength(0);
            final String object = (st.getObject() instanceof URI)
                    ? sb.append('<').append(st.getObject().toString()).append('>').toString()
                    : st.getObject().toString();
            if (label != null && predicate.equals(RDFS_LABEL))
                label.add(object);
            if (description != null && predicate.equals(DC_DESCRIPTION))
                description.add(object);
            if (description != null && predicate.equals(DBP_ABSTRACT))
                description.add(object);
            if (types != null && predicate.equals(RDF_TYPE)) {
                types.add(object);
            } else {
                ConcurrentSkipListSet<String> hs = map.get(predicate);
                final String toAdd = isOut ? object : subject;
                if (hs == null) {
                    hs = new ConcurrentSkipListSet<String>();
                    map.put(predicate, hs);
                }
                if (hs.size() < 65535) // 2 ^ 16 - 1
                    hs.add(toAdd);
            }
        }
    } catch (RDFParseException e1) {
    } catch (RDFHandlerException e1) {
    } catch (IOException e1) {
    }
    triples.setLength(0);
}