use of java.util.concurrent.ArrayBlockingQueue in project OsmAnd-tools by osmandapp.
the class OsmDbAccessor method iterateOverEntities.
public int iterateOverEntities(IProgress progress, EntityType type, OsmDbVisitor visitor, boolean realCounts) throws SQLException, InterruptedException {
PreparedStatement select;
int count = 0;
if (realCounts) {
Statement statement = dbConn.createStatement();
computeRealCounts(statement);
statement.close();
}
BlockingQueue<Entity> toProcess = new ArrayBlockingQueue<Entity>(100000);
AbstractProducer entityProducer = null;
if (type == EntityType.NODE) {
// filter out all nodes without tags
select = iterateNodes;
count = allNodes;
} else if (type == EntityType.WAY) {
select = iterateWays;
count = allWays;
} else if (type == EntityType.WAY_BOUNDARY) {
select = iterateWayBoundaries;
count = allBoundaries;
} else {
select = iterateRelations;
count = allRelations;
}
entityProducer = new EntityProducer(toProcess, type, select);
progress.startWork(count);
// produce
entityProducer.start();
try {
// wait a little before starting taking entities from queue
Thread.sleep(150);
} catch (InterruptedException e) {
}
Entity entityToProcess = null;
Entity endEntity = entityProducer.getEndingEntity();
while ((entityToProcess = toProcess.take()) != endEntity) {
if (progress != null) {
progress.progress(1);
}
visitor.iterateEntity(entityToProcess, this);
}
return count;
}
use of java.util.concurrent.ArrayBlockingQueue in project karaf by apache.
the class Activator method createQueue.
@SuppressWarnings("unchecked")
private BlockingQueue<EventImpl> createQueue() throws Exception {
String type = getString(QUEUE_TYPE, null);
int size = getInt(QUEUE_SIZE, 1024);
if ("ArrayBlockingQueue".equals(type)) {
return new ArrayBlockingQueue<>(size);
} else if ("DisruptorBlockingQueue".equals(type)) {
return new DisruptorBlockingQueue(size);
} else if (type != null) {
logger.warn("Unknown queue type: " + type + "");
}
try {
return new DisruptorBlockingQueue(size);
} catch (NoClassDefFoundError t) {
return new ArrayBlockingQueue<>(size);
}
}
use of java.util.concurrent.ArrayBlockingQueue in project java-tron by tronprotocol.
the class TMessageHandle method init.
public void init() {
int n = Runtime.getRuntime().availableProcessors();
executors = new ThreadPoolExecutor(n, n, 0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(10000));
}
use of java.util.concurrent.ArrayBlockingQueue in project twister2 by DSC-SPIDAL.
the class BlockQueueMain method main.
public static void main(String[] args) throws InterruptedException {
BlockingQueue q1 = new PriorityBlockingQueue();
BlockingQueue q = new ArrayBlockingQueue(5);
Task task1 = new Task(1001, "Task 1", new Date(), "scripts/task_scripts/run-task1.sh", "Thread 1");
Task task2 = new Task(1002, "Task 2", new Date(), "scripts/task_scripts/run-task2.sh", "Thread 1");
Task task3 = new Task(1003, "Task 3", new Date(), "scripts/task_scripts/run-task3.sh", "Thread 1");
Task task4 = new Task(1004, "Task 4", new Date(), "scripts/task_scripts/run-task4.sh", "Thread 1");
Task task5 = new Task(1005, "Task 5", new Date(), "scripts/task_scripts/run-task5.sh", "Thread 1");
ArrayList<Task> taskArrayList = new ArrayList<>();
taskArrayList.add(task1);
taskArrayList.add(task2);
taskArrayList.add(task3);
taskArrayList.add(task4);
taskArrayList.add(task5);
// Demo Pub/Sup BlockingQueue
/*Producer p = new Producer(q);
Consumer c1 = new Consumer(q);
Consumer c2 = new Consumer(q);
Thread t = new Thread(p);
t.start();
Thread t1 = new Thread(c1);
t1.start();
System.out.println("Consumer : "+t1.getState());
System.out.println("Producer : "+t.getState());
*/
// Producing and Consuming real resources
/*Producer p = new Producer(q, task1);
Consumer c1 = new Consumer(q);
Consumer c2 = new Consumer(q);
Thread t = new Thread(p);
t.start();
Thread t1 = new Thread(c1);
t1.start();
System.out.println("Consumer : "+t1.getState());
System.out.println("Producer : "+t.getState());
*/
// consuming list of tasks
StaticProducer p = new StaticProducer(q, taskArrayList, TaskOps.LIST);
p.create();
q = p.getQueue();
System.out.println("Generated Queue : " + q.size());
Consumer c1 = new Consumer(q, TaskOps.LIST);
// Consumer c2 = new Consumer(q, TaskOps.LIST);
Thread t1 = new Thread(c1);
t1.start();
System.out.println("Consumer : " + t1.getState());
}
use of java.util.concurrent.ArrayBlockingQueue in project incubator-gobblin by apache.
the class CouchbaseWriter method write.
@Override
public Future<WriteResponse> write(final D record, final WriteCallback callback) {
assertRecordWritable(record);
if (record instanceof TupleDocument) {
((TupleDocument) record).content().value1().retain();
}
Observable<D> observable = _bucket.async().upsert(record);
if (callback == null) {
return new WriteResponseFuture<>(observable.timeout(_operationTimeout, _operationTimeunit).toBlocking().toFuture(), _defaultWriteResponseMapper);
} else {
final AtomicBoolean callbackFired = new AtomicBoolean(false);
final BlockingQueue<Pair<WriteResponse, Throwable>> writeResponseQueue = new ArrayBlockingQueue<>(1);
final Future<WriteResponse> writeResponseFuture = new Future<WriteResponse>() {
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
return false;
}
@Override
public boolean isCancelled() {
return false;
}
@Override
public boolean isDone() {
return callbackFired.get();
}
@Override
public WriteResponse get() throws InterruptedException, ExecutionException {
Pair<WriteResponse, Throwable> writeResponseThrowablePair = writeResponseQueue.take();
return getWriteResponseorThrow(writeResponseThrowablePair);
}
@Override
public WriteResponse get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
Pair<WriteResponse, Throwable> writeResponseThrowablePair = writeResponseQueue.poll(timeout, unit);
if (writeResponseThrowablePair == null) {
throw new TimeoutException("Timeout exceeded while waiting for future to be done");
} else {
return getWriteResponseorThrow(writeResponseThrowablePair);
}
}
};
observable.timeout(_operationTimeout, _operationTimeunit).subscribe(new Subscriber<D>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
callbackFired.set(true);
writeResponseQueue.add(new Pair<WriteResponse, Throwable>(null, e));
callback.onFailure(e);
}
@Override
public void onNext(D doc) {
try {
callbackFired.set(true);
WriteResponse writeResponse = new GenericWriteResponse<D>(doc);
writeResponseQueue.add(new Pair<WriteResponse, Throwable>(writeResponse, null));
callback.onSuccess(writeResponse);
} finally {
if (doc instanceof TupleDocument) {
((TupleDocument) doc).content().value1().release();
}
}
}
});
return writeResponseFuture;
}
}
Aggregations