Example usage for java.util.concurrent ArrayBlockingQueue ArrayBlockingQueue

List of usage examples for java.util.concurrent ArrayBlockingQueue ArrayBlockingQueue

Introduction

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

Prototype

public ArrayBlockingQueue(int capacity) 

Source Link

Document

Creates an ArrayBlockingQueue with the given (fixed) capacity and default access policy.

Usage

From source file:org.languagetool.rules.AbstractCompoundRule.java

@Override
public RuleMatch[] match(AnalyzedSentence sentence) {
    List<RuleMatch> ruleMatches = new ArrayList<>();
    AnalyzedTokenReadings[] tokens = getSentenceWithImmunization(sentence).getTokensWithoutWhitespace();

    RuleMatch prevRuleMatch = null;//from   w ww .j av a 2 s.  c  om
    Queue<AnalyzedTokenReadings> prevTokens = new ArrayBlockingQueue<>(MAX_TERMS);
    for (int i = 0; i < tokens.length + MAX_TERMS - 1; i++) {
        AnalyzedTokenReadings token;
        // we need to extend the token list so we find matches at the end of the original list:
        if (i >= tokens.length) {
            token = new AnalyzedTokenReadings(new AnalyzedToken("", "", null), prevTokens.peek().getStartPos());
        } else {
            token = tokens[i];
        }
        if (i == 0) {
            addToQueue(token, prevTokens);
            continue;
        } else if (token.isImmunized()) {
            continue;
        }

        AnalyzedTokenReadings firstMatchToken = prevTokens.peek();
        List<String> stringsToCheck = new ArrayList<>();
        List<String> origStringsToCheck = new ArrayList<>(); // original upper/lowercase spelling
        Map<String, AnalyzedTokenReadings> stringToToken = getStringToTokenMap(prevTokens, stringsToCheck,
                origStringsToCheck);
        // iterate backwards over all potentially incorrect strings to make
        // sure we match longer strings first:
        for (int k = stringsToCheck.size() - 1; k >= 0; k--) {
            String stringToCheck = stringsToCheck.get(k);
            String origStringToCheck = origStringsToCheck.get(k);
            if (getCompoundRuleData().getIncorrectCompounds().contains(stringToCheck)) {
                AnalyzedTokenReadings atr = stringToToken.get(stringToCheck);
                String msg = null;
                List<String> replacement = new ArrayList<>();
                if (!getCompoundRuleData().getNoDashSuggestion().contains(stringToCheck)) {
                    replacement.add(origStringToCheck.replace(' ', '-'));
                    msg = withHyphenMessage;
                }
                if (isNotAllUppercase(origStringToCheck)
                        && !getCompoundRuleData().getOnlyDashSuggestion().contains(stringToCheck)) {
                    replacement.add(mergeCompound(origStringToCheck,
                            getCompoundRuleData().getNoDashLowerCaseSuggestion().stream()
                                    .anyMatch(s -> origStringsToCheck.contains(s))));
                    msg = withoutHyphenMessage;
                }
                String[] parts = stringToCheck.split(" ");
                if (parts.length > 0 && parts[0].length() == 1) {
                    replacement.clear();
                    replacement.add(origStringToCheck.replace(' ', '-'));
                    msg = withHyphenMessage;
                } else if (replacement.isEmpty() || replacement.size() == 2) { // isEmpty shouldn't happen
                    msg = withOrWithoutHyphenMessage;
                }
                RuleMatch ruleMatch = new RuleMatch(this, sentence, firstMatchToken.getStartPos(),
                        atr.getEndPos(), msg, shortDesc);
                ruleMatch.setSuggestedReplacements(replacement);
                // avoid duplicate matches:
                if (prevRuleMatch != null && prevRuleMatch.getFromPos() == ruleMatch.getFromPos()) {
                    prevRuleMatch = ruleMatch;
                    break;
                }
                prevRuleMatch = ruleMatch;
                ruleMatches.add(ruleMatch);
                break;
            }
        }
        addToQueue(token, prevTokens);
    }
    return toRuleMatchArray(ruleMatches);
}

From source file:ubic.gemma.loader.genome.goldenpath.GoldenPathBioSequenceLoader.java

/**
 * @param inputStream//from  w  w  w  . j  a  va 2s.c o  m
 * @return
 * @throws IOException
 */
public void load(final InputStream inputStream) {

    final BlockingQueue<BioSequence> queue = new ArrayBlockingQueue<BioSequence>(QUEUE_SIZE);
    final SecurityContext context = SecurityContextHolder.getContext();

    Thread loadThread = new Thread(new Runnable() {
        @Override
        public void run() {
            log.info("Starting loading");
            SecurityContextHolder.setContext(context);
            load(queue);
        }
    });

    loadThread.start();

    Thread parseThread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                parser.parse(inputStream, queue);
            } catch (IOException e) {
                e.printStackTrace();
            }
            log.info("Done parsing");
            producerDone = true;
        }
    });

    parseThread.start();

    while (!producerDone || !consumerDone) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

From source file:com.pinterest.rocksplicator.controller.WorkerPoolTest.java

@Test
public void testCancelSingleTask() throws Exception {
    Semaphore idleWorkersSemaphore = new Semaphore(0);
    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(1));
    WorkerPool workerPool = new WorkerPool(threadPoolExecutor, idleWorkersSemaphore, new TaskQueue() {
    });//from w w w.  j  av a  2 s  .  c o  m
    Task task = getSleepIncrementTask();
    workerPool.assignTask(task);
    Thread.sleep(10);
    workerPool.abortTask(task.clusterName);
    Thread.sleep(100);
    Assert.assertEquals(1, idleWorkersSemaphore.availablePermits());
}

From source file:org.apache.kylin.cube.inmemcubing.InMemCubeBuilderTest.java

private void testBuildInner() throws Exception {

    InMemCubeBuilder cubeBuilder = new InMemCubeBuilder(cube.getDescriptor(), dictionaryMap);
    //DoggedCubeBuilder cubeBuilder = new DoggedCubeBuilder(cube.getDescriptor(), dictionaryMap);
    cubeBuilder.setConcurrentThreads(nThreads);

    ArrayBlockingQueue<List<String>> queue = new ArrayBlockingQueue<List<String>>(1000);
    ExecutorService executorService = Executors.newSingleThreadExecutor();

    try {/*from ww  w .  j  a v a  2s  .  com*/
        // round 1
        {
            Future<?> future = executorService
                    .submit(cubeBuilder.buildAsRunnable(queue, new ConsoleGTRecordWriter()));
            feedData(cube, flatTable, queue, nInpRows);
            future.get();
        }

        // round 2, zero input
        {
            Future<?> future = executorService
                    .submit(cubeBuilder.buildAsRunnable(queue, new ConsoleGTRecordWriter()));
            feedData(cube, flatTable, queue, 0);
            future.get();
        }

        // round 3
        {
            Future<?> future = executorService
                    .submit(cubeBuilder.buildAsRunnable(queue, new ConsoleGTRecordWriter()));
            feedData(cube, flatTable, queue, nInpRows);
            future.get();
        }

    } catch (Exception e) {
        logger.error("stream build failed", e);
        throw new IOException("Failed to build cube ", e);
    }
}

From source file:org.mozilla.gecko.background.fxa.TestAccountLoader.java

/**
 * Runs a Loader synchronously and returns the result of the load. The loader will
 * be started, stopped, and destroyed by this method so it cannot be reused.
 *
 * @param loader The loader to run synchronously
 * @return The result from the loader/*w ww.  ja v  a2s  .  co  m*/
 */
public <T> T getLoaderResultSynchronously(final Loader<T> loader) {
    // The test thread blocks on this queue until the loader puts it's result in
    final ArrayBlockingQueue<AtomicReference<T>> queue = new ArrayBlockingQueue<AtomicReference<T>>(1);

    // This callback runs on the "main" thread and unblocks the test thread
    // when it puts the result into the blocking queue
    final OnLoadCompleteListener<T> listener = new OnLoadCompleteListener<T>() {
        @Override
        public void onLoadComplete(Loader<T> completedLoader, T data) {
            // Shut the loader down
            completedLoader.unregisterListener(this);
            completedLoader.stopLoading();
            completedLoader.reset();
            // Store the result, unblocking the test thread
            queue.add(new AtomicReference<T>(data));
        }
    };

    // This handler runs on the "main" thread of the process since AsyncTask
    // is documented as needing to run on the main thread and many Loaders use
    // AsyncTask
    final Handler mainThreadHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            loader.registerListener(0, listener);
            loader.startLoading();
        }
    };

    // Ask the main thread to start the loading process
    mainThreadHandler.sendEmptyMessage(0);

    // Block on the queue waiting for the result of the load to be inserted
    T result;
    while (true) {
        try {
            result = queue.take().get();
            break;
        } catch (InterruptedException e) {
            throw new RuntimeException("waiting thread interrupted", e);
        }
    }
    return result;
}

From source file:com.kurento.kmf.media.HttpGetEndpointAsyncTest.java

/**
 * Test for {@link MediaSessionStartedEvent}
 * // w  ww  . j  a  v  a 2s  . c o  m
 * @throws InterruptedException
 */
@Test
public void testEventMediaSessionStarted() throws InterruptedException {

    final PlayerEndpoint player = pipeline.newPlayerEndpoint(URL_SMALL).build();
    player.connect(httpEp);

    final CountDownLatch eosLatch = new CountDownLatch(1);
    player.addEndOfStreamListener(new MediaEventListener<EndOfStreamEvent>() {

        @Override
        public void onEvent(EndOfStreamEvent event) {
            eosLatch.countDown();
        }
    });

    final BlockingQueue<ListenerRegistration> events = new ArrayBlockingQueue<ListenerRegistration>(1);
    httpEp.addMediaSessionStartedListener(new MediaEventListener<MediaSessionStartedEvent>() {

        @Override
        public void onEvent(MediaSessionStartedEvent event) {
            player.play();
        }
    }, new Continuation<ListenerRegistration>() {

        @Override
        public void onSuccess(ListenerRegistration result) {
            events.add(result);
        }

        @Override
        public void onError(Throwable cause) {
            throw new KurentoMediaFrameworkException(cause);
        }
    });

    ListenerRegistration reg = events.poll(500, MILLISECONDS);
    Assert.assertNotNull(reg);

    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {
        // This should trigger MediaSessionStartedEvent
        httpclient.execute(new HttpGet(httpEp.getUrl()));
    } catch (ClientProtocolException e) {
        throw new KurentoMediaFrameworkException(e);
    } catch (IOException e) {
        throw new KurentoMediaFrameworkException(e);
    }

    try {
        eosLatch.await(500, MILLISECONDS);
    } catch (InterruptedException e) {
        player.release();
        throw new KurentoMediaFrameworkException(e);
    }

}

From source file:org.ambud.marauder.source.ids.IDSFileSource.java

@Override
public void configure(Context context) {
    super.configure(context);
    context = new Context(context.getSubProperties(getPrefix()));
    this.watchDirectory = new File(context.getString(PROP_DIRECTORY, getDefaultDirectory()));
    this.logBaseName = context.getString(PROP_BASE_NAME, getDefaultFilename());
    this.isSequential = context.getBoolean(PROP_IS_SEQUENTIAL, true);
    logger.info("Snort Source will spool/watch - " + this.watchDirectory.getAbsolutePath()
            + " for Snort log files whose names start with:" + this.logBaseName);
    FileSystemManager fsMgr = null;/*from w w  w .j a  v  a 2s .  c o m*/
    try {
        fsMgr = VFS.getManager();
    } catch (FileSystemException e) {
        Throwables.propagate(e);
    }
    try {
        this.watchObject = fsMgr.resolveFile(watchDirectory.getAbsolutePath());
    } catch (FileSystemException e) {
        Throwables.propagate(e);
    }
    this.monitor = new DefaultFileMonitor(new FileListener() {

        @Override
        public void fileChanged(FileChangeEvent arg0) throws Exception {
            // ignore these
        }

        @Override
        public void fileCreated(FileChangeEvent fileEvent) throws Exception {
            if (acceptFile(fileEvent.getFile().getName().getBaseName())) {
                logger.info("Acknowledged new file:" + fileEvent.getFile().getName().getPath());
                builderFileReader(fileEvent.getFile(), true);
            }
        }

        @Override
        public void fileDeleted(FileChangeEvent arg0) throws Exception {
            // acknowledge these
        }

    });
    int bufferSize = context.getInteger(PROP_BUFFER_SIZE, 500);
    this.outputQueue = new ArrayBlockingQueue<MarauderIDSEvent>(bufferSize);
}

From source file:at.salzburgresearch.vgi.vgianalyticsframework.activityanalysis.pipeline.impl.VgiPipelineImpl.java

/**
 * Starts the pipeline. Create producers, lets them read the VGI operations, receives the data and sends the operations to a set of consumers
 *///w w w  .  j av a  2 s .c om
@Override
public void start() {
    timerStart = new Date();

    BlockingQueue<IVgiFeature> queue = new ArrayBlockingQueue<IVgiFeature>(queueSize);

    /** Create thread(s) which will read the PBF files */
    Thread[] producerThread = new Thread[numThreads];
    for (int i = 0; i < numThreads; i++) {

        producerName = (settings.isReadQuadtree()) ? "vgiOperationPbfReaderQuadtree" : "vgiOperationPbfReader";

        producer = ctx.getBean(producerName, IVgiAnalysisPipelineProducer.class);
        producer.setQueue(queue);
        producerThread[i] = new Thread(producer);

        producer.setProducerCount(numThreads);
        producer.setProducerNumber(i);
        producer.setPbfDataFolder((pbfDataFolder != null) ? pbfDataFolder : settings.getPbfDataFolder());

        producer.setFilterNodeId(filterNodeId);
        producer.setFilterWayId(filterWayId);
        producer.setFilterRelationId(filterRelationId);
        producer.setFilterGeometryType(filterGeometryType);
        producer.setConstrainedFilter(constrainedFilter);
        producer.setCoordinateOnly(coordinateOnly);

        producer.setFilterFileId(filterFileId);

        producerThread[i].start();
    }

    List<IVgiFeature> currentBatch = new ArrayList<IVgiFeature>();

    try {
        doBeforeFirstBatch();

        /** Read queue as long as it is not empty */
        boolean resumePipeline = true;
        while (resumePipeline) {
            resumePipeline = false;
            for (int i = 0; i < numThreads; i++) {
                if (producerThread[i].isAlive())
                    resumePipeline = true;
            }
            if (!queue.isEmpty())
                resumePipeline = true;
            if (!resumePipeline)
                break;

            IVgiFeature currentFeature = queue.poll(60, TimeUnit.MILLISECONDS);

            if (currentFeature == null)
                continue;

            /** Detach batch if minimum batch size is reached */
            if (currentBatch.size() >= batchSize) {
                detachBatch(currentBatch);
                currentBatch.clear();
            }

            currentBatch.add(currentFeature);
        }

        if (currentBatch.size() > 0) {
            detachBatch(currentBatch);
        }

        doAfterLastBatch();

    } catch (InterruptedException e) {
        log.error("error joining producer thread", e);
    }

    if (settings.getActionAnalyzerList() != null) {
        writeMetaData(settings.getResultFolder());
    }
}

From source file:com.predic8.membrane.core.interceptor.schemavalidation.SchematronValidator.java

public SchematronValidator(ResolverMap resourceResolver, String schematron,
        ValidatorInterceptor.FailureHandler failureHandler, Router router, BeanFactory beanFactory)
        throws Exception {
    this.failureHandler = failureHandler;

    //works as standalone "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl"
    TransformerFactory fac;/*from  w  ww  .  j  a va2 s  .com*/
    try {
        fac = beanFactory.getBean("transformerFactory", TransformerFactory.class);
    } catch (NoSuchBeanDefinitionException e) {
        throw new RuntimeException(
                "Please define a bean called 'transformerFactory' in monitor-beans.xml, e.g. with "
                        + "<spring:bean id=\"transformerFactory\" class=\"com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl\" />",
                e);
    }
    fac.setURIResolver(new URIResolver() {
        @Override
        public Source resolve(String href, String base) throws TransformerException {
            return new StreamSource(SchematronValidator.class.getResourceAsStream(href));
        }
    });
    Transformer t = fac.newTransformer(
            new StreamSource(SchematronValidator.class.getResourceAsStream("conformance1-5.xsl")));

    // transform schematron-XML into XSLT
    DOMResult r = new DOMResult();
    t.transform(new StreamSource(router.getResolverMap().resolve(schematron)), r);

    // build XSLT transformers
    fac.setURIResolver(null);
    int concurrency = Runtime.getRuntime().availableProcessors() * 2;
    transformers = new ArrayBlockingQueue<Transformer>(concurrency);
    for (int i = 0; i < concurrency; i++) {
        Transformer transformer = fac.newTransformer(new DOMSource(r.getNode()));
        transformer.setErrorListener(new NullErrorListener()); // silence console logging
        transformers.put(transformer);
    }

    xmlInputFactory = XMLInputFactory.newInstance();
    xmlInputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
    xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
}

From source file:com.aol.advertising.qiao.injector.file.watcher.QiaoFileManager.java

public void init() throws Exception {
    _validate();//from w w  w.  j a v a 2 s  .co  m

    if (queueCapacity <= 0)
        queueCapacity = DEFAULT_QUEUE_CAPACITY;

    queue = new ArrayBlockingQueue<FileOperationEvent>(queueCapacity);

    srcDirPath = Paths.get(srcDir);

    if (filesPatternForRenameOnInit == null)
        filesPatternForRenameOnInit = donefilePattern;

    _setupFileFinder();

    _setupDoneFileHandler();

    _setupFileRenameMatcher();

    _setupQuarantineFileHandler();

    logger.info(this.getClass().getSimpleName() + " initialized");

}