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);
}
}
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);
}
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);
}
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;
}
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);
}
Aggregations