Search in sources :

Example 66 with ArrayBlockingQueue

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;
}
Also used : Entity(net.osmand.osm.edit.Entity) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement)

Example 67 with ArrayBlockingQueue

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);
    }
}
Also used : ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) DisruptorBlockingQueue(com.conversantmedia.util.concurrent.DisruptorBlockingQueue)

Example 68 with ArrayBlockingQueue

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));
}
Also used : ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 69 with ArrayBlockingQueue

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());
}
Also used : ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) BlockingQueue(java.util.concurrent.BlockingQueue) PriorityBlockingQueue(java.util.concurrent.PriorityBlockingQueue) Task(edu.iu.dsc.tws.executor.model.Task) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ArrayList(java.util.ArrayList) PriorityBlockingQueue(java.util.concurrent.PriorityBlockingQueue) Date(java.util.Date)

Example 70 with ArrayBlockingQueue

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;
    }
}
Also used : WriteResponseFuture(org.apache.gobblin.writer.WriteResponseFuture) WriteResponse(org.apache.gobblin.writer.WriteResponse) GenericWriteResponse(org.apache.gobblin.writer.GenericWriteResponse) TupleDocument(org.apache.gobblin.couchbase.common.TupleDocument) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) WriteResponseFuture(org.apache.gobblin.writer.WriteResponseFuture) Future(java.util.concurrent.Future) TimeUnit(java.util.concurrent.TimeUnit) Pair(org.apache.commons.math3.util.Pair) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)440 Test (org.junit.Test)158 ArrayList (java.util.ArrayList)75 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)74 IOException (java.io.IOException)66 CountDownLatch (java.util.concurrent.CountDownLatch)58 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)41 BlockingQueue (java.util.concurrent.BlockingQueue)34 ExecutorService (java.util.concurrent.ExecutorService)34 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)31 List (java.util.List)29 LocalAddress (io.netty.channel.local.LocalAddress)27 HashMap (java.util.HashMap)25 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)25 Subscription (rx.Subscription)25 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)24 HttpInitiator (org.jocean.http.client.HttpClient.HttpInitiator)23 File (java.io.File)22 CompletableFuture (java.util.concurrent.CompletableFuture)22 LinkedList (java.util.LinkedList)21