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.tallison.cc.WReGetter.java

private void execute(String[] args) throws IOException {
    if (args.length != 3) {
        usage();/*from   ww  w  . j a v a2  s  .c  o  m*/
        System.exit(1);
    }
    if (args[0].contains("-h")) {
        usage();
        System.exit(0);
    }
    int numThreads = Integer.parseInt(args[0]);
    BufferedReader r = Files.newBufferedReader(Paths.get(args[1]));
    ArrayBlockingQueue<DigestURLPair> queue = new ArrayBlockingQueue<DigestURLPair>(1000);
    QueueFiller filler = new QueueFiller(r, queue, numThreads);
    new Thread(filler).start();
    rootDir = Paths.get(args[2]);
    System.out.println("creating thread pool");
    ExecutorService executorService = Executors.newFixedThreadPool(numThreads);
    ExecutorCompletionService<Integer> executorCompletionService = new ExecutorCompletionService<Integer>(
            executorService);
    System.out.println("about to start");

    for (int i = 0; i < numThreads; i++) {
        System.out.println("submitted " + i);
        executorCompletionService.submit(new WGetter(queue));
    }

    int completed = 0;
    while (completed < numThreads) {
        try {
            Future<Integer> future = executorCompletionService.poll(1, TimeUnit.SECONDS);
            if (future != null) {
                completed++;
            }
        } catch (InterruptedException e) {

        }
    }
    executorService.shutdown();
    executorService.shutdownNow();
    System.exit(0);

}

From source file:eu.stratosphere.nephele.taskmanager.bufferprovider.GlobalBufferPool.java

/**
 * Constructs the global buffer pool./*w  w w  . java 2s .com*/
 */
private GlobalBufferPool() {

    this.numberOfBuffers = GlobalConfiguration.getInteger(ConfigConstants.TASK_MANAGER_NETWORK_NUM_BUFFERS_KEY,
            ConfigConstants.DEFAULT_TASK_MANAGER_NETWORK_NUM_BUFFERS);
    this.bufferSizeInBytes = GlobalConfiguration.getInteger(
            ConfigConstants.TASK_MANAGER_NETWORK_BUFFER_SIZE_KEY,
            ConfigConstants.DEFAULT_TASK_MANAGER_NETWORK_BUFFER_SIZE);

    this.buffers = new ArrayBlockingQueue<MemorySegment>(this.numberOfBuffers);

    // Initialize buffers
    for (int i = 0; i < this.numberOfBuffers; i++) {
        // allocate byteBuffer
        final byte[] segMemory = new byte[this.bufferSizeInBytes];
        final MemorySegment readBuffer = new MemorySegment(segMemory);
        this.buffers.add(readBuffer);
    }

    LOG.info("Initialized global buffer pool with " + this.numberOfBuffers + " buffers with a size "
            + this.bufferSizeInBytes + " bytes each");
}

From source file:com.l2jfree.gameserver.ThreadPoolManager.java

private ThreadPoolManager() {
    final int instantPoolSize = Math.max(1, Config.THREAD_POOL_SIZE / 3);

    _scheduledPool = new ScheduledThreadPoolExecutor(Config.THREAD_POOL_SIZE - instantPoolSize);
    _scheduledPool.setRejectedExecutionHandler(new L2RejectedExecutionHandler());
    _scheduledPool.prestartAllCoreThreads();

    _instantPool = new ThreadPoolExecutor(instantPoolSize, instantPoolSize, 0, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(100000));
    _instantPool.setRejectedExecutionHandler(new L2RejectedExecutionHandler());
    _instantPool.prestartAllCoreThreads();

    _longRunningPool = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS,
            new SynchronousQueue<Runnable>());
    _longRunningPool.setRejectedExecutionHandler(new L2RejectedExecutionHandler());
    _longRunningPool.prestartAllCoreThreads();

    scheduleAtFixedRate(new Runnable() {
        @Override//from   w w  w  .  j  a  va2 s .c om
        public void run() {
            purge();
        }
    }, 60000, 60000);

    _log.info("ThreadPoolManager: Initialized with " + _scheduledPool.getPoolSize() + " scheduler, "
            + _instantPool.getPoolSize() + " instant, " + _longRunningPool.getPoolSize()
            + " long running thread(s).");
}

From source file:com.moss.veracity.core.load.operation.OperationManager.java

public OperationManager(OperationFactory factory, OperationListener listener, int threads, long stagger) {
    this.factory = factory;
    this.listener = listener;
    this.threads = threads;
    this.stagger = stagger;

    this.queue = new ArrayBlockingQueue<Operation>(threads);
    this.workers = new ArrayList<OperationWorker>(threads);
}

From source file:org.ngrinder.perftest.service.ConsoleManager.java

/**
 * Prepare console queue.//w w w . j av a  2s .c om
 */
@PostConstruct
public void init() {
    int consoleSize = getConsoleSize();
    consoleQueue = new ArrayBlockingQueue<ConsoleEntry>(consoleSize);
    final String currentIP = config.getCurrentIP();
    for (int each : getAvailablePorts(currentIP, consoleSize, getConsolePortBase(), MAX_PORT_NUMBER)) {
        final ConsoleEntry e = new ConsoleEntry(config.getCurrentIP(), each);
        try {
            e.occupySocket();
            consoleQueue.add(e);
        } catch (Exception ex) {
            LOG.error("socket binding to {}:{} is failed", config.getCurrentIP(), each);
        }

    }
}

From source file:com.taobao.tdhs.client.statement.BatchStatementImpl.java

public TDHSResponse[] commit() throws TDHSException {
    ByteArrayOutputStream retData = new ByteArrayOutputStream(2 * 1024);
    long headerId = id.getAndIncrement();
    try {/*from   www.j a v  a 2s .c  o m*/
        try {
            for (internal_struct is : batchRequest) {
                retData.write(is.getPacket().toByteArray());
                responses.put(is.getPacket().getSeqId(), new ArrayBlockingQueue<BasePacket>(1));
            }
        } catch (IOException e) {
            throw new TDHSException(e);
        }
        BasePacket headerPacket = new BasePacket(TDHSCommon.RequestType.BATCH, headerId, batchRequest.size(),
                retData.toByteArray());
        ArrayBlockingQueue<BasePacket> queue = new ArrayBlockingQueue<BasePacket>(1);
        responses.put(headerId, queue);
        tdhsNet.write(headerPacket);
        return do_real_response(queue);
    } finally {
        responses.remove(headerId);
        for (internal_struct is : batchRequest) {
            responses.remove(is.getPacket().getSeqId());
        }
        batchRequest.clear();
    }
}

From source file:org.apache.cxf.systest.jaxrs.JAXRSContinuationsTest.java

private void doTestContinuation(String pathSegment) throws Exception {
    ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 5, 0, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(10));
    CountDownLatch startSignal = new CountDownLatch(1);
    CountDownLatch doneSignal = new CountDownLatch(5);

    executor.execute(new BookWorker("http://localhost:" + PORT + "/bookstore/" + pathSegment + "/1", "1",
            "CXF in Action1", startSignal, doneSignal));
    executor.execute(new BookWorker("http://localhost:" + PORT + "/bookstore/" + pathSegment + "/2", "2",
            "CXF in Action2", startSignal, doneSignal));
    executor.execute(new BookWorker("http://localhost:" + PORT + "/bookstore/" + pathSegment + "/3", "3",
            "CXF in Action3", startSignal, doneSignal));
    executor.execute(new BookWorker("http://localhost:" + PORT + "/bookstore/" + pathSegment + "/4", "4",
            "CXF in Action4", startSignal, doneSignal));
    executor.execute(new BookWorker("http://localhost:" + PORT + "/bookstore/" + pathSegment + "/5", "5",
            "CXF in Action5", startSignal, doneSignal));

    startSignal.countDown();//from  w w  w .j a v a2  s.  c  o m
    doneSignal.await(60, TimeUnit.SECONDS);
    executor.shutdownNow();
    assertEquals("Not all invocations have completed", 0, doneSignal.getCount());
}

From source file:com.scvngr.levelup.core.test.SupportLoaderTestCase.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  w  w .  jav a2 s . c o  m*/
 */
public <T> T getLoaderResultSynchronously(final Loader<T> loader) {
    // The test thread blocks on this queue until the loader puts its result in
    final ArrayBlockingQueue<T> queue = new ArrayBlockingQueue<T>(1);
    final CountDownLatch latch = new CountDownLatch(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(final Loader<T> completedLoader, final T data) {
            // Shut the loader down
            completedLoader.unregisterListener(this);
            completedLoader.stopLoading();
            completedLoader.reset();

            // Store the result, unblocking the test thread
            if (null != data) {
                queue.add(data);
            }
            latch.countDown();
        }
    };

    // 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(final 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 {
            latch.await();
            result = queue.peek();
            break;
        } catch (final InterruptedException e) {
            throw new RuntimeException("waiting thread interrupted", e);
        }
    }

    return result;
}

From source file:org.apache.servicemix.jbi.runtime.impl.ComponentContextImpl.java

public ComponentContextImpl(ComponentRegistryImpl componentRegistry, ComponentWrapper component,
        Map<String, ?> properties) {
    super(componentRegistry);
    this.component = component;
    this.properties = properties;
    this.queue = new ArrayBlockingQueue<Exchange>(DEFAULT_QUEUE_CAPACITY);
    this.componentEndpoint = new EndpointImpl(properties);
    this.componentEndpoint.setQueue(queue);
    this.componentRegistry.getNmr().getEndpointRegistry().register(componentEndpoint, properties);
    this.dc = new DeliveryChannelImpl(this, componentEndpoint.getChannel(), queue);
    this.name = (String) properties.get(ComponentRegistry.NAME);
    this.workspaceRoot = new File(System.getProperty("servicemix.base"), "data/jbi/" + name + "/workspace");
    this.workspaceRoot.mkdirs();
    this.installRoot = new File(System.getProperty("servicemix.base"), "data/jbi/" + name + "/install");
    this.installRoot.mkdirs();
}

From source file:org.bimserver.tools.ifcloader.BulkLoader.java

private void start() {
    Path basePath = Paths.get("C:\\Bulk");
    Path bulkPath = basePath.resolve("bulk");
    Path regularPath = basePath.resolve("single");
    try (JsonBimServerClientFactory factory = new JsonBimServerClientFactory("http://localhost:8080")) {
        ExecutorService executorService = new ThreadPoolExecutor(16, 16, 1, TimeUnit.HOURS,
                new ArrayBlockingQueue<>(10000));
        try (BimServerClient client = factory
                .create(new UsernamePasswordAuthenticationInfo("admin@bimserver.org", "admin"))) {
            if (Files.exists(bulkPath)) {
                DirectoryStream<Path> stream = Files.newDirectoryStream(bulkPath);
                for (Path path : stream) {
                    executorService.submit(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                SProject project = client.getServiceInterface()
                                        .addProject(path.getFileName().toString(), "ifc2x3tc1");
                                client.bulkCheckin(project.getOid(), path, "Automatic bulk checkin");
                            } catch (ServerException e) {
                                e.printStackTrace();
                            } catch (UserException e) {
                                e.printStackTrace();
                            } catch (PublicInterfaceNotFoundException e) {
                                e.printStackTrace();
                            }//from   w  ww . java2s  .  c  o  m
                        }
                    });
                }
            }
            if (Files.exists(regularPath)) {
                DirectoryStream<Path> regularStream = Files.newDirectoryStream(regularPath);
                for (Path regularFile : regularStream) {
                    executorService.submit(new Runnable() {
                        @Override
                        public void run() {
                            String filename = regularFile.getFileName().toString().toLowerCase();
                            try {
                                if (filename.endsWith(".ifc") || filename.endsWith(".ifczip")) {
                                    String schema = client.getServiceInterface().determineIfcVersion(
                                            extractHead(regularFile),
                                            filename.toLowerCase().endsWith(".ifczip"));
                                    SProject project = client.getServiceInterface().addProject(filename,
                                            schema);
                                    SDeserializerPluginConfiguration deserializer = client.getServiceInterface()
                                            .getSuggestedDeserializerForExtension("ifc", project.getOid());
                                    client.checkinSync(project.getOid(), "Automatic checkin",
                                            deserializer.getOid(), false, regularFile);
                                } else {
                                    LOGGER.info("Skipping " + filename);
                                }
                            } catch (Exception e) {
                                LOGGER.error(filename, e);
                            }
                        }
                    });
                }
            }
            executorService.shutdown();
            executorService.awaitTermination(24, TimeUnit.HOURS);
        }
    } catch (BimServerClientException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}