Example usage for java.util Collections synchronizedList

List of usage examples for java.util Collections synchronizedList

Introduction

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

Prototype

public static <T> List<T> synchronizedList(List<T> list) 

Source Link

Document

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

Usage

From source file:com.moneydance.modules.features.importlist.io.FileAdmin.java

public FileAdmin(final String baseDirectory, final FeatureModuleContext argContext) {
    this.localizable = Helper.INSTANCE.getLocalizable();
    this.context = argContext;
    if (SystemUtils.IS_OS_MAC) {
        this.directoryChooser = new MacOSDirectoryChooser(baseDirectory);
    } else {//from   w ww  .  jav  a  2 s. c o m
        this.directoryChooser = new DefaultDirectoryChooser(baseDirectory);
    }
    this.directoryValidator = DirectoryValidator.INSTANCE;
    this.transactionFileFilter = new SuffixFileFilter(
            Helper.INSTANCE.getSettings().getTransactionFileExtensions(), IOCase.INSENSITIVE);
    this.textFileFilter = new SuffixFileFilter(Helper.INSTANCE.getSettings().getTextFileExtensions(),
            IOCase.INSENSITIVE);
    this.readableFileFilter = FileFilterUtils.and(CanReadFileFilter.CAN_READ,
            FileFilterUtils.or(this.transactionFileFilter, this.textFileFilter));

    this.listener = new TransactionFileListener();
    this.listener.addObserver(this);
    this.monitor = new FileAlterationMonitor(Helper.INSTANCE.getSettings().getMonitorInterval());

    this.files = Collections.synchronizedList(new ArrayList<File>());
}

From source file:com.alibaba.otter.node.etl.conflict.impl.FileBatchConflictDetectServiceImpl.java

/**
 * ?//from  w w w.j a va 2 s.  c  o  m
 */
private FileBatch onFileConflictDetect(FileConflictDetectEvent event) {
    final FileBatch fileBatch = event.getFileBatch();
    if (CollectionUtils.isEmpty(fileBatch.getFiles())) {
        return fileBatch;
    }

    ExecutorTemplate executorTemplate = executorTemplateGetter.get();
    try {
        MDC.put(OtterConstants.splitPipelineLoadLogFileKey,
                String.valueOf(fileBatch.getIdentity().getPipelineId()));
        executorTemplate.start();
        // ?poolSize
        Pipeline pipeline = configClientService.findPipeline(fileBatch.getIdentity().getPipelineId());
        executorTemplate.adjustPoolSize(pipeline.getParameters().getFileLoadPoolSize());
        // ?
        final List<FileData> result = Collections.synchronizedList(new ArrayList<FileData>());
        final List<FileData> filter = Collections.synchronizedList(new ArrayList<FileData>());
        for (final FileData source : fileBatch.getFiles()) {
            EventType type = source.getEventType();
            if (type.isDelete()) {
                result.add(source);
            } else {
                executorTemplate.submit(new Runnable() {

                    public void run() {
                        MDC.put(OtterConstants.splitPipelineLoadLogFileKey,
                                String.valueOf(fileBatch.getIdentity().getPipelineId()));
                        // ?
                        String namespace = source.getNameSpace();
                        String path = source.getPath();
                        FileData target = null;

                        int count = 0;
                        while (count++ < retry) {// ??
                            try {
                                if (true == StringUtils.isBlank(namespace)) {
                                    // local file
                                    java.io.File targetFile = new java.io.File(path);
                                    if (true == targetFile.exists()) {
                                        // modified time cost
                                        long lastModified = targetFile.lastModified();
                                        long size = targetFile.length();
                                        // ?
                                        target = new FileData();
                                        target.setLastModifiedTime(lastModified);
                                        target.setSize(size);
                                    }
                                } else {
                                    // remote file
                                    throw new RuntimeException(source + " is not support!");
                                }

                                break; // ?
                            } catch (Exception ex) {
                                target = null;
                            }
                        }

                        boolean shouldSync = false;
                        if (target != null) {
                            if (true == accept(target, source)) {
                                shouldSync = true;
                            }
                        } else {
                            shouldSync = true;
                        }

                        if (true == shouldSync) {
                            result.add(source);
                        } else {
                            filter.add(source);
                        }
                    }
                });
            }
        }
        // ??
        executorTemplate.waitForResult();

        if (pipeline.getParameters().getDumpEvent() && logger.isInfoEnabled()) {
            logger.info(FileloadDumper.dumpFilterFileDatas(fileBatch.getIdentity(), fileBatch.getFiles().size(),
                    result.size(), filter));
        }

        // 
        FileBatch target = new FileBatch();
        target.setIdentity(fileBatch.getIdentity());
        target.setFiles(result);
        return target;
    } finally {
        if (executorTemplate != null) {
            executorTemplateGetter.release(executorTemplate);
        }

        MDC.remove(OtterConstants.splitPipelineLoadLogFileKey);
    }
}

From source file:com.calamp.services.kinesis.events.processor.UnorderedRecordProcessor.java

/**
 * {@inheritDoc}// ww w .  ja  v  a  2  s .c o m
 */
@Override
public void processRecords(List<Record> records, IRecordProcessorCheckpointer checkpointer) {
    System.out.println("Process Unordered Records #" + records.size());
    List<CalAmpEvent> eventsOldEnough = Collections.synchronizedList(new ArrayList<CalAmpEvent>());
    List<CalAmpEvent> eventsTooYoung = Collections.synchronizedList(new ArrayList<CalAmpEvent>());
    for (Record r : records) {
        Utils.lazyLog(r, CalAmpParameters.unorderdStreamName, CalAmpParameters.bufferLogName);
        // The bytes could be null if there is an issue with the JSON serialization by the Jackson JSON library.
        byte[] bytes = r.getData().array();
        if (bytes != null) {
            CalAmpEvent e = CalAmpEvent.fromJsonAsBytes(r.getData().array());
            if (e != null) {
                if (!(eventsOldEnough.contains(e))) {
                    if (CalAmpEventFilter.oldEnough(e)) {
                        eventsOldEnough.add(e);
                    } else if (!(eventsTooYoung.contains(e))) {
                        eventsTooYoung.add(e);
                    }
                }
            } else {
                LOG.warn("Skipping record. Unable to parse record into CalAmpEvent 2. Partition Key: "
                        + r.getPartitionKey() + " Event: " + e);
            }
        } else {
            LOG.warn("Skipping record. Unable to parse record into CalAmpEvent 1. Partition Key: "
                    + r.getPartitionKey());
        }
    }

    Collections.sort(eventsOldEnough, new CalAmpEventPriorityComparator());
    Collections.sort(eventsTooYoung, new CalAmpEventPriorityComparator());

    for (CalAmpEvent cae : eventsTooYoung) {
        LOG.info("Event too young : " + cae);

    }
    for (CalAmpEvent cae : eventsOldEnough) {
        LOG.info("Event old enough: " + cae);
    }

    Utils.putByParts(eventsTooYoung, CalAmpParameters.unorderdStreamName, kinesisClientToUnordered,
            CalAmpParameters.bufferLogName);
    Utils.putByParts(eventsOldEnough, CalAmpParameters.orderedStreamName, kinesisClientToOrdered,
            CalAmpParameters.bufferLogName);
    checkpoint(checkpointer);
}

From source file:swp.bibclient.Network.java

/**
 * Fragt bei einem Server nach den Medien an.
 *
 * @return Die Liste der Medien./*from  w ww . ja va2  s.c o  m*/
 * @throws IOException
 *             Kann geworfen werden bei IO-Problemen.
 */
public final List<Medium> getMediums() throws IOException {
    DefaultHttpClient httpClient = new DefaultHttpClient();

    // Setzen eines ConnectionTimeout von 2 Sekunden:
    HttpParams params = httpClient.getParams();
    params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
    httpClient.setParams(params);

    // Der BibServer sollte lokal laufen, daher zugriff auf Localhost
    // 10.0.2.2 fr den AndroidEmulator.
    HttpContext localContext = new BasicHttpContext();
    HttpGet httpGet = new HttpGet(server + mediumpath);
    String text = null;

    Log.i(Network.class.getName(), "Try to get a http connection...");
    HttpResponse response = httpClient.execute(httpGet, localContext);
    HttpEntity entity = response.getEntity();
    text = getASCIIContentFromEntity(entity);
    Log.i(Network.class.getName(), "Create new Gson object");
    Gson gson = new Gson();
    Log.i(Network.class.getName(), "Create listOfTestObject");
    // Wir arbeiten mit einem TypeToken um fr eine generische Liste wieder
    // Objekte zu erhalten.
    Type listOfTestObject = new TypeToken<List<Medium>>() {
    }.getType();

    Log.i(Network.class.getName(), "Convert to a list.");
    /*
     * Hier befindet sich ein unsicherer Cast, da wir nicht wirklich wissen,
     * ob der String, den wir von der Website bekommen, sich wirklich in
     * eine Liste von Bchern umwandeln lsst
     */
    try {
        @SuppressWarnings("unchecked")
        List<Medium> list = Collections.synchronizedList((List<Medium>) gson.fromJson(text, listOfTestObject));
        return list;
    } catch (ClassCastException e) {
        throw new ClientProtocolException("Returned type is not a list of book objects");
    }
}

From source file:org.springframework.integration.ip.tcp.connection.ConnectionFactoryTests.java

public void testObtainConnectionIds(AbstractServerConnectionFactory serverFactory) throws Exception {
    final List<IpIntegrationEvent> events = Collections.synchronizedList(new ArrayList<IpIntegrationEvent>());
    int expectedEvents = serverFactory instanceof TcpNetServerConnectionFactory ? 7 // Listening, + OPEN, CLOSE, EXCEPTION for each side
            : 5; // Listening, + OPEN, CLOSE (but we *might* get exceptions, depending on timing).
    final CountDownLatch serverListeningLatch = new CountDownLatch(1);
    final CountDownLatch eventLatch = new CountDownLatch(expectedEvents);
    ApplicationEventPublisher publisher = new ApplicationEventPublisher() {

        @Override//from  www .  ja  v a 2 s  .  c  o m
        public void publishEvent(ApplicationEvent event) {
            LogFactory.getLog(this.getClass()).trace("Received: " + event);
            events.add((IpIntegrationEvent) event);
            if (event instanceof TcpConnectionServerListeningEvent) {
                serverListeningLatch.countDown();
            }
            eventLatch.countDown();
        }

        @Override
        public void publishEvent(Object event) {

        }

    };
    serverFactory.setBeanName("serverFactory");
    serverFactory.setApplicationEventPublisher(publisher);
    serverFactory = spy(serverFactory);
    final CountDownLatch serverConnectionInitLatch = new CountDownLatch(1);
    doAnswer(invocation -> {
        Object result = invocation.callRealMethod();
        serverConnectionInitLatch.countDown();
        return result;
    }).when(serverFactory).wrapConnection(any(TcpConnectionSupport.class));
    ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    scheduler.setPoolSize(10);
    scheduler.afterPropertiesSet();
    BeanFactory bf = mock(BeanFactory.class);
    when(bf.containsBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)).thenReturn(true);
    when(bf.getBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class))
            .thenReturn(scheduler);
    serverFactory.setBeanFactory(bf);
    TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
    adapter.setOutputChannel(new NullChannel());
    adapter.setConnectionFactory(serverFactory);
    adapter.start();
    assertTrue("Listening event not received", serverListeningLatch.await(10, TimeUnit.SECONDS));
    assertThat(events.get(0), instanceOf(TcpConnectionServerListeningEvent.class));
    assertThat(((TcpConnectionServerListeningEvent) events.get(0)).getPort(), equalTo(serverFactory.getPort()));
    int port = serverFactory.getPort();
    TcpNetClientConnectionFactory clientFactory = new TcpNetClientConnectionFactory("localhost", port);
    clientFactory.registerListener(message -> false);
    clientFactory.setBeanName("clientFactory");
    clientFactory.setApplicationEventPublisher(publisher);
    clientFactory.start();
    TcpConnectionSupport client = clientFactory.getConnection();
    List<String> clients = clientFactory.getOpenConnectionIds();
    assertEquals(1, clients.size());
    assertTrue(clients.contains(client.getConnectionId()));
    assertTrue("Server connection failed to register", serverConnectionInitLatch.await(1, TimeUnit.SECONDS));
    List<String> servers = serverFactory.getOpenConnectionIds();
    assertEquals(1, servers.size());
    assertTrue(serverFactory.closeConnection(servers.get(0)));
    servers = serverFactory.getOpenConnectionIds();
    assertEquals(0, servers.size());
    int n = 0;
    clients = clientFactory.getOpenConnectionIds();
    while (n++ < 100 && clients.size() > 0) {
        Thread.sleep(100);
        clients = clientFactory.getOpenConnectionIds();
    }
    assertEquals(0, clients.size());
    assertTrue(eventLatch.await(10, TimeUnit.SECONDS));
    assertThat("Expected at least " + expectedEvents + " events; got: " + events.size() + " : " + events,
            events.size(), greaterThanOrEqualTo(expectedEvents));

    FooEvent event = new FooEvent(client, "foo");
    client.publishEvent(event);
    assertThat("Expected at least " + expectedEvents + " events; got: " + events.size() + " : " + events,
            events.size(), greaterThanOrEqualTo(expectedEvents + 1));

    try {
        event = new FooEvent(mock(TcpConnectionSupport.class), "foo");
        client.publishEvent(event);
        fail("Expected exception");
    } catch (IllegalArgumentException e) {
        assertTrue("Can only publish events with this as the source".equals(e.getMessage()));
    }

    SocketAddress address = serverFactory.getServerSocketAddress();
    if (address instanceof InetSocketAddress) {
        InetSocketAddress inetAddress = (InetSocketAddress) address;
        assertEquals(port, inetAddress.getPort());
    }
    serverFactory.stop();
    scheduler.shutdown();
}

From source file:org.apache.synapse.transport.nhttp.ConnectionPool.java

public void release(NHttpClientConnection conn) {

    HttpContext ctx = conn.getContext();
    Axis2HttpRequest axis2Req = (Axis2HttpRequest) ctx.getAttribute(ClientHandler.AXIS2_HTTP_REQUEST);
    HttpRoute route = axis2Req.getRoute();

    List<NHttpClientConnection> connections = null;
    synchronized (connMap) {
        // use double locking to make sure
        connections = (List<NHttpClientConnection>) connMap.get(route);
        if (connections == null) {
            connections = Collections.synchronizedList(new LinkedList<NHttpClientConnection>());
            connMap.put(route, connections);
        }//  www . j av a2 s.  c  o  m
    }

    cleanConnectionReferences(conn);
    connections.add(conn);

    if (log.isDebugEnabled()) {
        log.debug("Released a connection : " + route + " to the connection pool of current size : "
                + connections.size());
    }
}

From source file:org.apache.jackrabbit.core.security.user.MembershipCacheTest.java

public void testConcurrency() throws Exception {
    Stats stats = new Stats();
    List<Exception> exceptions = Collections.synchronizedList(new ArrayList<Exception>());
    List<Reader> readers = new ArrayList<Reader>();
    for (int i = 0; i < NUM_READERS; i++) {
        Reader r = new Reader(createSession(), stats, exceptions);
        r.addUser(TEST_USER_PREFIX + 0);
        readers.add(r);/*ww w . j a v  a2s .  co m*/
    }
    Node test = session.getRootNode().addNode("test", "nt:unstructured");
    session.save();
    for (Reader r : readers) {
        r.start();
    }
    for (int i = 1; i < NUM_USERS; i++) {
        test.addNode("node-" + i);
        session.save();
        for (Reader r : readers) {
            r.addUser(TEST_USER_PREFIX + i);
        }
    }
    for (Reader r : readers) {
        r.join();
    }
    test.remove();
    session.save();
    System.out.println(stats);
    for (Exception e : exceptions) {
        throw e;
    }
}

From source file:org.apache.roller.weblogger.business.referrers.ReferrerQueueManagerImpl.java

@com.google.inject.Inject
protected ReferrerQueueManagerImpl(Weblogger roller) {

    mLogger.info("Instantiating Referrer Queue Manager");

    this.roller = roller;

    // lookup config options
    this.asyncMode = WebloggerConfig.getBooleanProperty("referrers.asyncProcessing.enabled");

    mLogger.info("Asynchronous referrer processing = " + this.asyncMode);

    if (this.asyncMode) {

        String num = WebloggerConfig.getProperty("referrers.queue.numWorkers");
        String sleep = WebloggerConfig.getProperty("referrers.queue.sleepTime");

        try {//from   ww w  .ja  v a 2 s.  c  o  m
            this.numWorkers = Integer.parseInt(num);

            if (numWorkers < 1)
                this.numWorkers = 1;

        } catch (NumberFormatException nfe) {
            mLogger.warn("Invalid num workers [" + num + "], using default");
        }

        try {
            // multiply by 1000 because we expect input in seconds
            this.sleepTime = Integer.parseInt(sleep) * 1000;
        } catch (NumberFormatException nfe) {
            mLogger.warn("Invalid sleep time [" + sleep + "], using default");
        }

        // create the processing queue
        this.referrerQueue = Collections.synchronizedList(new ArrayList());

        // start up workers
        this.workers = new ArrayList();
        ContinuousWorkerThread worker = null;
        QueuedReferrerProcessingJob job = null;
        for (int i = 0; i < this.numWorkers; i++) {
            job = new QueuedReferrerProcessingJob();
            worker = new ContinuousWorkerThread("ReferrerWorker" + i, job, this.sleepTime);
            workers.add(worker);
            worker.start();
        }
    }
}

From source file:edu.cmu.lti.oaqa.bioasq.concept.retrieval.GoPubMedConceptRetrievalExecutor.java

@Override
public void process(JCas jcas) throws AnalysisEngineProcessException {
    AbstractQuery aquery = TypeUtil.getAbstractQueries(jcas).stream().findFirst().get();
    String queryString = bopQueryStringConstructor.construct(aquery).replaceAll("[^A-Za-z0-9_\\-\"]+", " ");
    LOG.info("Query string: {}", queryString);
    List<ConceptSearchResult> concepts = Collections.synchronizedList(new ArrayList<>());
    ExecutorService es = Executors.newCachedThreadPool();
    for (BioASQUtil.Ontology ontology : BioASQUtil.Ontology.values()) {
        es.execute(() -> {/*from   ww  w .j a  v  a 2s .c o  m*/
            try {
                concepts.addAll(BioASQUtil.searchOntology(service, jcas, queryString, pages, hits, ontology));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
    }
    es.shutdown();
    try {
        if (!es.awaitTermination(timeout, TimeUnit.MINUTES)) {
            LOG.warn("Timeout occurs for one or some concept retrieval services.");
        }
    } catch (InterruptedException e) {
        throw new AnalysisEngineProcessException(e);
    }
    Map<String, List<ConceptSearchResult>> onto2concepts = concepts.stream()
            .collect(groupingBy(ConceptSearchResult::getSearchId));
    for (Map.Entry<String, List<ConceptSearchResult>> entry : onto2concepts.entrySet()) {
        List<ConceptSearchResult> results = entry.getValue();
        LOG.info("Retrieved {} concepts from {}", results.size(), entry.getKey());
        if (LOG.isDebugEnabled()) {
            results.stream().limit(3).forEach(c -> LOG.debug(" - {}", TypeUtil.toString(c)));
        }
    }
    TypeUtil.rankedSearchResultsByScore(concepts, limit).forEach(ConceptSearchResult::addToIndexes);
}

From source file:com.almende.pi5.common.agents.LoggerAgent.java

/**
 * Clear.
 */
public void clear() {
    logs = Collections.synchronizedList(new ArrayList<LogLine>());
}