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:strat.mining.multipool.stats.builder.CoinsolverStatsBuilder.java

public CoinsolverStatsBuilder() {
    currentAddresses = Collections.synchronizedSet(new HashSet<String>());
}

From source file:channellistmaker.listmaker.EPGListMaker.java

/**
 * EPG XML????????/* w ww  .  ja  va 2  s .  c om*/
 *
 * @return ???????Document????????
 */
public synchronized Set<Document> seek() {
    Set<Document> EPGs = Collections.synchronizedSet(new HashSet<Document>());
    List<File> FL = this.seeker.seek();
    for (File F : FL) {
        Document d = new XMLLoader(charset).Load(F);
        if (d != null) {
            LOG.info("EPG?????? EPG FILE = " + F.toString());
            EPGs.add(d);
        } else {
            LOG.warn(
                    "EPG?????????????? EPG FILE = "
                            + F.toString());
        }
    }
    return Collections.unmodifiableSet(EPGs);
}

From source file:org.apache.geode.internal.process.signal.AbstractSignalNotificationHandler.java

protected AbstractSignalNotificationHandler() {
    for (Signal signal : Signal.values()) {
        signalListeners.put(signal, Collections.synchronizedSet(new HashSet<>()));
    }//  ww w .j  av a2s . com
    // NOTE uncomment for debugging purposes...
    // debug();
}

From source file:io.wcm.caravan.pipeline.impl.JsonPipelineMultipleSubscriptionsTest.java

@Test
public void subscribeConcurrentlyToPlainPipelineOutputs() throws InterruptedException, JSONException {
    firstStep = newPipelineWithResponseBody("{id:123}");

    // use a synchronized set to collect the pipeline output from multiple threads
    Set<JsonPipelineOutput> distinctOutputs = Collections.synchronizedSet(new HashSet<JsonPipelineOutput>());

    // create multiple simultaneous threads that subscribe to the same pipeline output
    // and use a CountDownLatch to delay the subscription until all threads have been started
    ExecutorService executorService = Executors.newCachedThreadPool();
    CountDownLatch countDown = new CountDownLatch(100);
    while (countDown.getCount() > 0) {

        executorService.submit(() -> {

            countDown.await();/* ww w. j a v  a 2s. c o  m*/
            distinctOutputs.add(firstStep.getOutput().toBlocking().single());

            return null; // this is required for the lambda to be considered a Callable<Void> and therefore be allowed to throw exceptions
        });

        countDown.countDown();
    }

    executorService.shutdown();
    executorService.awaitTermination(1, TimeUnit.MINUTES);

    // ensure all threads received the same JsonPipelineOutput instance with the expected JSON output
    assertEquals(1, distinctOutputs.size());
    JSONAssert.assertEquals("{id: 123}", firstStep.getStringOutput().toBlocking().first(),
            JSONCompareMode.STRICT);
}

From source file:org.springframework.integration.test.coverage.AbstractChannelCoverageTests.java

@Before
public void _setUp() {
    _applicationContext = _ctx;/*  www . j  ava 2s . c  o m*/
    String theClassName = this.getClass().getSimpleName();
    Package thePackage = this.getClass().getPackage();
    try {
        file = new RandomAccessFile(COVERAGE_FILE, "rw");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
    if (aggregatePackageTests ? !thePackage.equals(currentPackage) : !theClassName.equals(simpleClassName)) {
        traversedChannels = Collections.synchronizedSet(new HashSet<String>());
        if (file != null) {
            try {
                seekPosition = file.length();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
    }
    findAndInterceptChannels();
    simpleClassName = theClassName;
    currentPackage = thePackage;
}

From source file:org.codehaus.cargo.website.WebsiteGenerator.java

private static void download() throws Exception {
    long start = System.currentTimeMillis();

    File attachmentsDirectory = new File("target", "attachments");
    if (!attachmentsDirectory.isDirectory()) {
        attachmentsDirectory.mkdirs();/*from   w w w .j  a v a  2 s. c om*/
    }
    File sourceDirectory = new File("target", "source");
    if (!sourceDirectory.isDirectory()) {
        sourceDirectory.mkdirs();
    }
    File tempDirectory = new File("target", "temp");
    if (!tempDirectory.isDirectory()) {
        tempDirectory.mkdirs();
    }

    URL url = new URL("https://codehaus-cargo.atlassian.net/wiki/rest/api/space/CARGO/content?limit=2048");
    URLConnection connection = url.openConnection();
    StringBuilder sb = new StringBuilder();
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
        for (String line = reader.readLine(); line != null; line = reader.readLine()) {
            sb.append(line);
        }
    }

    JSONArray pages = new JSONObject(sb.toString()).getJSONObject("page").getJSONArray("results");
    System.out.println("Found " + pages.length() + " pages to handle");
    files = Collections.synchronizedSet(new HashSet<File>(pages.length()));
    exceptions = Collections.synchronizedMap(new HashMap<URL, Exception>());
    attachments = Collections.synchronizedSet(new HashSet<URL>(pages.length()));
    for (int i = 0; i < pages.length(); i++) {
        JSONObject links = pages.getJSONObject(i).getJSONObject("_links");
        WebsiteGenerator runnable = new WebsiteGenerator();
        runnable.url = new URL(links.getString("self") + "?expand=body.view");
        Thread thread = new Thread(runnable);
        executor.submit(thread);
    }

    JSONArray blogposts = new JSONObject(sb.toString()).getJSONObject("blogpost").getJSONArray("results");
    System.out.println("Found " + blogposts.length() + " blog posts to handle");
    files = Collections.synchronizedSet(new HashSet<File>(blogposts.length()));
    exceptions = Collections.synchronizedMap(new HashMap<URL, Exception>());
    attachments = Collections.synchronizedSet(new HashSet<URL>(blogposts.length()));
    for (int i = 0; i < blogposts.length(); i++) {
        JSONObject links = blogposts.getJSONObject(i).getJSONObject("_links");
        WebsiteGenerator runnable = new WebsiteGenerator();
        runnable.url = new URL(links.getString("self") + "?expand=body.view");
        Thread thread = new Thread(runnable);
        executor.submit(thread);
    }

    while (executor.getCompletedTaskCount() < pages.length() + blogposts.length() + attachments.size()) {
        Thread.sleep(5000);
        System.out.println("  - Completed " + executor.getCompletedTaskCount() + "/"
                + (pages.length() + blogposts.length() + attachments.size()) + " tasks - "
                + ((System.currentTimeMillis() - start) / 1000) + " seconds spent so far");
    }
    if (executor.getCompletedTaskCount() < pages.length() + blogposts.length() + attachments.size()) {
        throw new Exception("WARNING: Only completed " + executor.getCompletedTaskCount() + " tasks out of "
                + (pages.length() + blogposts.length() + attachments.size()));
    }
    System.out.println("All tasks complete");
    for (File file : files) {
        System.out.println("  - Wrote file " + file.getAbsolutePath());
    }
    if (exceptions.size() > 0) {
        for (HashMap.Entry<URL, Exception> exception : exceptions.entrySet()) {
            System.out.println("  - Pending exception for URL " + exception.getKey() + ": " + exception);
        }
        throw new Exception("Some files have failed download");
    }
    System.out.println(
            "Export completed, total time taken " + ((System.currentTimeMillis() - start) / 1000) + " seconds");
}

From source file:strat.mining.multipool.stats.builder.CoinshiftStatsBuilder.java

public CoinshiftStatsBuilder() {
    currentAddresses = Collections.synchronizedSet(new HashSet<String>());
}

From source file:org.punksearch.logic.online.OnlineStatuses.java

public Set<String> getOnline(Collection<String> hosts) {
    Iterator<String> iter = hosts.iterator();
    Set<String> onlineHosts = Collections.synchronizedSet(new HashSet<String>());
    List<Thread> threadList = new ArrayList<Thread>();
    for (int i = 0; i < PROBE_THREAD_COUNT; i++) {
        Thread thread = new OnlineCheckThread("OnlineCheckThread" + i, iter, onlineHosts);
        thread.start();/* ww w .  j a  va 2  s  . co m*/
        threadList.add(thread);
    }
    try {
        for (Thread thread : threadList) {
            thread.join();
        }
    } catch (InterruptedException e) {
        // TODO: log
        onlineHosts = new HashSet<String>();
    }
    log.debug("getOnline: " + hosts.size() + " -> " + onlineHosts.size());
    return onlineHosts;
}

From source file:strat.mining.multipool.stats.builder.WaffleStatsBuilder.java

public WaffleStatsBuilder() {
    currentAddresses = Collections.synchronizedSet(new HashSet<String>());
}

From source file:mitm.djigzo.web.pages.admin.LoggerManager.java

@SetupRender
@Secured({ FactoryRoles.ROLE_ADMIN })/*from  w w w.j  av  a 2 s.c  o  m*/
public void setupGrid() {
    if (selected == null) {
        selected = Collections.synchronizedSet(new HashSet<String>());
    } else {
        selected.clear();
    }
}