Search in sources :

Example 91 with LinkedBlockingQueue

use of java.util.concurrent.LinkedBlockingQueue in project async-http-client by AsyncHttpClient.

the class AsyncStreamLifecycleTest method testStream.

@Test(groups = "standalone")
public void testStream() throws Exception {
    try (AsyncHttpClient ahc = asyncHttpClient()) {
        final AtomicBoolean err = new AtomicBoolean(false);
        final LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>();
        final AtomicBoolean status = new AtomicBoolean(false);
        final AtomicInteger headers = new AtomicInteger(0);
        final CountDownLatch latch = new CountDownLatch(1);
        ahc.executeRequest(ahc.prepareGet(getTargetUrl()).build(), new AsyncHandler<Object>() {

            public void onThrowable(Throwable t) {
                fail("Got throwable.", t);
                err.set(true);
            }

            public State onBodyPartReceived(HttpResponseBodyPart e) throws Exception {
                if (e.length() != 0) {
                    String s = new String(e.getBodyPartBytes());
                    logger.info("got part: {}", s);
                    queue.put(s);
                }
                return State.CONTINUE;
            }

            public State onStatusReceived(HttpResponseStatus e) throws Exception {
                status.set(true);
                return State.CONTINUE;
            }

            public State onHeadersReceived(HttpResponseHeaders e) throws Exception {
                if (headers.incrementAndGet() == 2) {
                    throw new Exception("Analyze this.");
                }
                return State.CONTINUE;
            }

            public Object onCompleted() throws Exception {
                latch.countDown();
                return null;
            }
        });
        assertTrue(latch.await(1, TimeUnit.SECONDS), "Latch failed.");
        assertFalse(err.get());
        assertEquals(queue.size(), 2);
        assertTrue(queue.contains("part1"));
        assertTrue(queue.contains("part2"));
        assertTrue(status.get());
        assertEquals(headers.get(), 1);
    }
}
Also used : LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.testng.annotations.Test)

Example 92 with LinkedBlockingQueue

use of java.util.concurrent.LinkedBlockingQueue in project android_packages_inputmethods_LatinIME by CyanogenMod.

the class DictionaryService method onCreate.

@Override
public void onCreate() {
    // By default, a thread pool executor does not timeout its core threads, so it will
    // never kill them when there isn't any work to do any more. That would mean the service
    // can never die! By creating it this way and calling allowCoreThreadTimeOut, we allow
    // the single thread to time out after WORKER_THREAD_TIMEOUT_SECONDS = 15 seconds, allowing
    // the process to be reclaimed by the system any time after that if it's not doing
    // anything else.
    // Executors#newSingleThreadExecutor creates a ThreadPoolExecutor but it returns the
    // superclass ExecutorService which does not have the #allowCoreThreadTimeOut method,
    // so we can't use that.
    mExecutor = new ThreadPoolExecutor(1, /* corePoolSize */
    1, /* maximumPoolSize */
    WORKER_THREAD_TIMEOUT_SECONDS, /* keepAliveTime */
    TimeUnit.SECONDS, /* unit for keepAliveTime */
    new LinkedBlockingQueue<Runnable>());
    mExecutor.allowCoreThreadTimeOut(true);
}
Also used : ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue)

Example 93 with LinkedBlockingQueue

use of java.util.concurrent.LinkedBlockingQueue in project ACS by ACS-Community.

the class ThreadCommunicatorTest method testConsistency.

public void testConsistency() throws Exception {
    ThreadCommunicator tc1 = ThreadCommunicator.getInstance();
    LinkedBlockingQueue tvn = tc1.createChannel("TVN");
    LinkedBlockingQueue chv = tc1.createChannel("CHV");
    assertEquals(tvn, tc1.getChannel("TVN"));
    assertEquals(chv, tc1.getChannel("CHV"));
    assertNotSame(tvn, chv);
}
Also used : ThreadCommunicator(cl.utfsm.samplingSystemUI.core.ThreadCommunicator) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue)

Example 94 with LinkedBlockingQueue

use of java.util.concurrent.LinkedBlockingQueue in project alluxio by Alluxio.

the class FileSystemMaster method startupCheckConsistency.

/**
   * Checks the consistency of the root in a multi-threaded and incremental fashion. This method
   * will only READ lock the directories and files actively being checked and release them after the
   * check on the file / directory is complete.
   *
   * @return a list of paths in Alluxio which are not consistent with the under storage
   * @throws InterruptedException if the thread is interrupted during execution
   * @throws IOException if an error occurs interacting with the under storage
   */
private List<AlluxioURI> startupCheckConsistency(final ExecutorService service) throws InterruptedException, IOException {
    /** A marker {@link StartupConsistencyChecker}s add to the queue to signal completion */
    final long completionMarker = -1;
    /** A shared queue of directories which have yet to be checked */
    final BlockingQueue<Long> dirsToCheck = new LinkedBlockingQueue<>();
    /**
     * A {@link Callable} which checks the consistency of a directory.
     */
    final class StartupConsistencyChecker implements Callable<List<AlluxioURI>> {

        /** The path to check, guaranteed to be a directory in Alluxio. */
        private final Long mFileId;

        /**
       * Creates a new callable which checks the consistency of a directory.
       * @param fileId the path to check
       */
        private StartupConsistencyChecker(Long fileId) {
            mFileId = fileId;
        }

        /**
       * Checks the consistency of the directory and all immediate children which are files. All
       * immediate children which are directories are added to the shared queue of directories to
       * check. The parent directory is READ locked during the entire call while the children are
       * READ locked only during the consistency check of the children files.
       *
       * @return a list of inconsistent uris
       * @throws IOException if an error occurs interacting with the under storage
       */
        @Override
        public List<AlluxioURI> call() throws IOException {
            List<AlluxioURI> inconsistentUris = new ArrayList<>();
            try (LockedInodePath dir = mInodeTree.lockFullInodePath(mFileId, InodeTree.LockMode.READ)) {
                Inode parentInode = dir.getInode();
                AlluxioURI parentUri = dir.getUri();
                if (!checkConsistencyInternal(parentInode, parentUri)) {
                    inconsistentUris.add(parentUri);
                }
                for (Inode childInode : ((InodeDirectory) parentInode).getChildren()) {
                    try {
                        childInode.lockReadAndCheckParent(parentInode);
                    } catch (InvalidPathException e) {
                        // This should be safe, continue.
                        LOG.debug("Error during startup check consistency, ignoring and continuing.", e);
                        continue;
                    }
                    try {
                        AlluxioURI childUri = parentUri.join(childInode.getName());
                        if (childInode.isDirectory()) {
                            dirsToCheck.add(childInode.getId());
                        } else {
                            if (!checkConsistencyInternal(childInode, childUri)) {
                                inconsistentUris.add(childUri);
                            }
                        }
                    } finally {
                        childInode.unlockRead();
                    }
                }
            } catch (FileDoesNotExistException e) {
                // This should be safe, continue.
                LOG.debug("A file scheduled for consistency check was deleted before the check.");
            } catch (InvalidPathException e) {
                // This should not happen.
                LOG.error("An invalid path was discovered during the consistency check, skipping.", e);
            }
            dirsToCheck.add(completionMarker);
            return inconsistentUris;
        }
    }
    // Add the root to the directories to check.
    dirsToCheck.add(mInodeTree.getRoot().getId());
    List<Future<List<AlluxioURI>>> results = new ArrayList<>();
    // Tracks how many checkers have been started.
    long started = 0;
    // Tracks how many checkers have completed.
    long completed = 0;
    do {
        Long fileId = dirsToCheck.take();
        if (fileId == completionMarker) {
            // A thread signaled completion.
            completed++;
        } else {
            // A new directory needs to be checked.
            StartupConsistencyChecker checker = new StartupConsistencyChecker(fileId);
            results.add(service.submit(checker));
            started++;
        }
    } while (started != completed);
    // Return the total set of inconsistent paths discovered.
    List<AlluxioURI> inconsistentUris = new ArrayList<>();
    for (Future<List<AlluxioURI>> result : results) {
        try {
            inconsistentUris.addAll(result.get());
        } catch (Exception e) {
            // This shouldn't happen, all futures should be complete.
            Throwables.propagate(e);
        }
    }
    service.shutdown();
    return inconsistentUris;
}
Also used : FileDoesNotExistException(alluxio.exception.FileDoesNotExistException) ArrayList(java.util.ArrayList) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) InvalidPathException(alluxio.exception.InvalidPathException) Callable(java.util.concurrent.Callable) AlluxioException(alluxio.exception.AlluxioException) BlockInfoException(alluxio.exception.BlockInfoException) FileAlreadyExistsException(alluxio.exception.FileAlreadyExistsException) IOException(java.io.IOException) InvalidPathException(alluxio.exception.InvalidPathException) AccessControlException(alluxio.exception.AccessControlException) InvalidFileSizeException(alluxio.exception.InvalidFileSizeException) FileDoesNotExistException(alluxio.exception.FileDoesNotExistException) FileAlreadyCompletedException(alluxio.exception.FileAlreadyCompletedException) DirectoryNotEmptyException(alluxio.exception.DirectoryNotEmptyException) UnexpectedAlluxioException(alluxio.exception.UnexpectedAlluxioException) LockedInodePath(alluxio.master.file.meta.LockedInodePath) InodeDirectory(alluxio.master.file.meta.InodeDirectory) Inode(alluxio.master.file.meta.Inode) Future(java.util.concurrent.Future) ArrayList(java.util.ArrayList) InodeLockList(alluxio.master.file.meta.InodeLockList) List(java.util.List) TtlBucketList(alluxio.master.file.meta.TtlBucketList) PrefixList(alluxio.collections.PrefixList) AlluxioURI(alluxio.AlluxioURI)

Example 95 with LinkedBlockingQueue

use of java.util.concurrent.LinkedBlockingQueue in project GNS by MobilityFirst.

the class ActiveHandler method main.

/***************** Test methods ****************/
/**
	 * @param args
	 * @throws JSONException 
	 * @throws ExecutionException 
	 * @throws InterruptedException 
	 */
public static void main(String[] args) throws JSONException, InterruptedException, ExecutionException {
    int numProcess = Integer.parseInt(args[0]);
    int numThread = Integer.parseInt(args[1]);
    boolean blocking = Boolean.parseBoolean(args[2]);
    if (numProcess <= 0) {
        System.out.println("Number of clients must be larger than 0.");
        System.exit(0);
    }
    final ThreadPoolExecutor executor;
    executor = new ThreadPoolExecutor(numProcess * numThread, numProcess * numThread, 0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
    executor.prestartAllCoreThreads();
    String guid = "4B48F507395639FD806459281C3C09BCBB16FDFF";
    String field = "someField";
    String noop_code = "";
    try {
        noop_code = new String(Files.readAllBytes(Paths.get("./scripts/activeCode/noop.js")));
    } catch (IOException e) {
        e.printStackTrace();
    }
    ValuesMap value = new ValuesMap();
    value.put(field, "someValue");
    // initialize a handler
    ActiveHandler handler = new ActiveHandler("", null, numProcess, numThread, blocking);
    ArrayList<Future<JSONObject>> tasks = new ArrayList<Future<JSONObject>>();
    int n = 1000000;
    long t1 = System.currentTimeMillis();
    for (int i = 0; i < n; i++) {
        tasks.add(executor.submit(new ActiveTask(clientPool[i % numProcess], guid, field, noop_code, value, 0)));
    }
    for (Future<JSONObject> task : tasks) {
        task.get();
    }
    long elapsed = System.currentTimeMillis() - t1;
    System.out.println("It takes " + elapsed + "ms, and the average latency for each operation is " + (elapsed * 1000.0 / n) + "us");
    System.out.println("The throughput is " + Util.df(n * 1000.0 / elapsed) + "reqs/sec");
    handler.shutdown();
    System.out.println(DelayProfiler.getStats());
    System.exit(0);
}
Also used : ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) JSONObject(org.json.JSONObject) Future(java.util.concurrent.Future) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Aggregations

LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)259 Test (org.junit.Test)91 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)64 IOException (java.io.IOException)26 ArrayList (java.util.ArrayList)23 Emitter (io.socket.emitter.Emitter)19 JSONObject (org.json.JSONObject)19 CountDownLatch (java.util.concurrent.CountDownLatch)18 ThreadFactory (java.util.concurrent.ThreadFactory)16 ExecutorService (java.util.concurrent.ExecutorService)14 BlockingQueue (java.util.concurrent.BlockingQueue)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 List (java.util.List)12 URI (java.net.URI)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 Intent (android.content.Intent)9 HashMap (java.util.HashMap)9 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)8 Map (java.util.Map)8 UUID (java.util.UUID)8