Example usage for java.util Collections synchronizedSet

List of usage examples for java.util Collections synchronizedSet

Introduction

In this page you can find the example usage for java.util Collections synchronizedSet.

Prototype

public static <T> Set<T> synchronizedSet(Set<T> s) 

Source Link

Document

Returns a synchronized (thread-safe) set backed by the specified set.

Usage

From source file:Main.java

/**
 * Constructs a new synchronized {@code Set} based on a {@link HashSet} with
 * the specified {@code initialCapacity}.
 * /*from  w ww. ja  v  a 2 s . c  o  m*/
 * @param initialCapacity
 *            the initial capacity of the set
 * 
 * @return a synchronized Set
 */
public static <T> Set<T> synchronizedSet(final int initialCapacity) {
    return Collections.synchronizedSet(new HashSet<T>(initialCapacity));
}

From source file:Main.java

public static Collection narrowSynchronizedCollection(Collection c) {
    if (c instanceof SortedSet)
        return Collections.synchronizedSortedSet((SortedSet) c);
    else if (c instanceof Set)
        return Collections.synchronizedSet((Set) c);
    else if (c instanceof List)
        return Collections.synchronizedList((List) c);
    else/*  w ww  .ja v  a 2s  .  c o m*/
        return Collections.synchronizedCollection(c);
}

From source file:Main.java

/**
 * Constructs a new synchronized {@code Set} based on a {@link HashSet}.
 * /*from   ww w .ja  v a  2 s  .  c o m*/
 * @return a synchronized Set
 */
public static <T> Set<T> synchronizedSet() {
    return Collections.synchronizedSet(Sets.<T>newHashSet());
}

From source file:com.poloure.simplerss.PagerAdapterTags.java

static List<String> getTagsFromIndex(Context context, Iterable<IndexItem> indexItems) {
    // Get the all tag from resources.
    String allTag = context.getString(R.string.all_tag);

    // Make the allTag the first tag.
    Set<String> tagSet = Collections.synchronizedSet(new LinkedHashSet<String>(0));
    tagSet.add(allTag);/*from w  w  w . j a  v  a  2 s.c o m*/

    if (null != indexItems) {
        for (IndexItem indexItem : indexItems) {
            tagSet.addAll(Arrays.asList(indexItem.m_tags));
        }
    }

    return new ArrayList<String>(tagSet);
}

From source file:Main.java

/**
 * Constructs a new synchronized {@code Set} based on a {@link HashSet} with
 * the specified {@code initialCapacity}.
 *
 * @param initialCapacity//from  ww w .j av a  2 s  .  co m
 *            the initial capacity of the set
 *
 * @return a synchronized Set
 */
public static <T> Set<T> synchronizedSet(int initialCapacity) {
    return Collections.synchronizedSet(new HashSet<T>(initialCapacity));
}

From source file:de.thischwa.pmcms.view.renderer.RenderData.java

public RenderData() {
    files = Collections.synchronizedSet(new HashSet<File>());
}

From source file:org.lable.oss.uniqueid.UniqueIDGeneratorThreadSafetyIT.java

@Test
public void multipleInstancesTest() throws InterruptedException {
    final Set<String> ids = Collections.synchronizedSet(new HashSet<String>());
    final int threadCount = 20;
    final int iterationCount = 10000;
    final CountDownLatch latch = new CountDownLatch(threadCount);

    // Generate IDs for the same generator-ID and cluster-ID in multiple threads.
    // Collision of IDs is almost guaranteed if the generator doesn't handle multi-threading gracefully.

    for (int i = 0; i < threadCount; i++) {
        Thread t = new Thread(new Runnable() {
            @Override/*from   www. jav  a 2s .  c  o  m*/
            public void run() {
                IDGenerator generator = LocalUniqueIDGeneratorFactory.generatorFor(1, 1);
                try {
                    for (int i = 0; i < iterationCount; i++) {
                        byte[] id = generator.generate();
                        String asHex = Hex.encodeHexString(id);
                        ids.add(asHex);
                    }
                } catch (GeneratorException e) {
                    // Test will fail due to missing IDs.
                    e.printStackTrace();
                }
                latch.countDown();
            }
        });
        t.start();
    }

    // Wait for all the threads to finish, or timeout.
    boolean successfullyUnlatched = latch.await(20, TimeUnit.SECONDS);
    assertThat(successfullyUnlatched, is(true));

    // If the set holds fewer items than this, duplicates were generated.
    assertThat(ids.size(), is(threadCount * iterationCount));
}

From source file:com.google.mr4c.algorithm.AlgorithmSchema.java

public AlgorithmSchema() {
    m_inputDatasets = Collections.synchronizedSet(new HashSet<String>());
    m_requiredInputDatasets = Collections.synchronizedSet(new HashSet<String>());
    m_optionalInputDatasets = Collections.synchronizedSet(new HashSet<String>());
    m_excludedInputDatasets = Collections.synchronizedSet(new HashSet<String>());
    m_outputDatasets = Collections.synchronizedSet(new HashSet<String>());
    m_expectedDimensions = Collections.synchronizedSet(new HashSet<DataKeyDimension>());
}

From source file:ExceptionCallbackMain.java

private void init(ExceptionListener[] initialGroup) {
    System.out.println("initializing...");

    exceptionListeners = Collections.synchronizedSet(new HashSet());

    if (initialGroup != null) {
        for (int i = 0; i < initialGroup.length; i++) {
            addExceptionListener(initialGroup[i]);
        }//ww w .  ja  v a2 s . c om
    }

    noStopRequested = true;

    Runnable r = new Runnable() {
        public void run() {
            try {
                runWork();
            } catch (Exception x) {
                sendException(x);
            }
        }
    };

    internalThread = new Thread(r);
    internalThread.start();
}

From source file:delfos.rs.contentbased.vsm.booleanvsm.SparseVector.java

public SparseVector(Collection<Key> domain) {
    map = Collections.synchronizedMap(new HashMap<>());

    this.domain = Collections.synchronizedSet(domain.stream().collect(Collectors.toSet()));
}