Example usage for java.util.concurrent LinkedBlockingQueue LinkedBlockingQueue

List of usage examples for java.util.concurrent LinkedBlockingQueue LinkedBlockingQueue

Introduction

In this page you can find the example usage for java.util.concurrent LinkedBlockingQueue LinkedBlockingQueue.

Prototype

public LinkedBlockingQueue() 

Source Link

Document

Creates a LinkedBlockingQueue with a capacity of Integer#MAX_VALUE .

Usage

From source file:com.mobicomkit.api.attachment.AttachmentManager.java

/**
 * Constructs the work queues and thread pools used to download and decode images.
 *///from   w w  w . j  a va2  s .  c o m
private AttachmentManager() {

    attachmentInProgress = new ArrayList<String>();
    attachmentTaskList = new ArrayList<AttachmentTask>();
    /*
     * Creates a work queue for the pool of Thread objects used for downloading, using a linked
     * list queue that blocks when the queue is empty.
     */
    mDownloadWorkQueue = new LinkedBlockingQueue<Runnable>();

    /*
     * Creates a work queue for the pool of Thread objects used for decoding, using a linked
     * list queue that blocks when the queue is empty.
     */
    mDecodeWorkQueue = new LinkedBlockingQueue<Runnable>();

    /*
     * Creates a work queue for the set of of task objects that control downloading and
     * decoding, using a linked list queue that blocks when the queue is empty.
     */
    mPhotoTaskWorkQueue = new LinkedBlockingQueue<AttachmentTask>();

    /*
     * Creates a new pool of Thread objects for the download work queue
     */
    mDownloadThreadPool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_TIME,
            KEEP_ALIVE_TIME_UNIT, mDownloadWorkQueue);

    /*
     * Creates a new pool of Thread objects for the decoding work queue
     */
    mDecodeThreadPool = new ThreadPoolExecutor(NUMBER_OF_CORES, NUMBER_OF_CORES, KEEP_ALIVE_TIME,
            KEEP_ALIVE_TIME_UNIT, mDecodeWorkQueue);

    // Instantiates a new cache based on the cache size estimate
    mPhotoCache = new LruCache<String, Bitmap>(IMAGE_CACHE_SIZE) {

        //            /*
        //             * This overrides the default sizeOf() implementation to return the
        //             * correct size of each cache entry.
        //             */
        //
        //            @Override
        //            protected int sizeOf(String paramURL, Bitmap image) {
        //                return image.getByteCount();
        //            }
    };
    /*
     * Instantiates a new anonymous Handler object and defines its
     * handleMessage() method. The Handler *must* run on the UI thread, because it moves photo
     * Bitmaps from the PhotoTask object to the View object.
     * To force the Handler to run on the UI thread, it's defined as part of the PhotoManager
     * constructor. The constructor is invoked when the class is first referenced, and that
     * happens when the View invokes startDownload. Since the View runs on the UI Thread, so
     * does the constructor and the Handler.
     */
    mHandler = new Handler(Looper.getMainLooper()) {

        /*
         * handleMessage() defines the operations to perform when the
         * Handler receives a new Message to process.
         */
        @Override
        public void handleMessage(Message inputMessage) {

            // Gets the image task from the incoming Message object.
            AttachmentTask attachmentTask = (AttachmentTask) inputMessage.obj;

            // Sets an PhotoView that's a weak reference to the
            // input ImageView
            AttachmentView localView = attachmentTask.getPhotoView();

            // If this input view isn't null
            if (localView != null) {

                /*
                 * Gets the URL of the *weak reference* to the input
                 * ImageView. The weak reference won't have changed, even if
                 * the input ImageView has.
                 */
                // URL localURL = localView.getLocation();

                /*
                 * Compares the URL of the input ImageView to the URL of the
                 * weak reference. Only updates the bitmap in the ImageView
                 * if this particular Thread is supposed to be serving the
                 * ImageView.
                 */
                //if (attachmentTask.getImageURL() == localURL)

                /*
                 * Chooses the action to take, based on the incoming message
                 *
                 */
                //TODO: show the status properly based on message status ...
                switch (inputMessage.what) {

                case DOWNLOAD_STARTED:
                    localView.getMessage().setAttDownloadInProgress(true);
                    localView.getProressBar().setVisibility(View.VISIBLE);
                    break;
                case DOWNLOAD_COMPLETE:
                    localView.getProressBar().setProgress(70);
                    localView.getMessage().setAttDownloadInProgress(false);
                    break;
                case DECODE_STARTED:
                    localView.getProressBar().setVisibility(View.VISIBLE);
                    localView.getProressBar().setProgress(90);
                    break;
                /*
                 * The decoding is done, so this sets the
                 * ImageView's bitmap to the bitmap in the
                 * incoming message
                 */
                case TASK_COMPLETE:

                    if (localView.getDownloadProgressLayout() != null
                            && !localView.getMessage().isAttachmentUploadInProgress()) {
                        localView.getDownloadProgressLayout().setVisibility(View.GONE);
                    } else if (localView.getProressBar() != null) {
                        localView.getProressBar().setVisibility(View.GONE);
                    }
                    localView.setImageBitmap(attachmentTask.getImage());
                    recycleTask(attachmentTask);
                    break;
                // The download failed, sets the background color to dark red
                case DOWNLOAD_FAILED:
                    //localView.setStatusResource(R.drawable.imagedownloadfailed);
                    localView.getProressBar().setProgress(0);
                    localView.getMessage().setAttDownloadInProgress(false);
                    localView.getDownloadProgressLayout().setVisibility(View.GONE);
                    localView.setVisibility(View.INVISIBLE);
                    Toast.makeText(localView.getContext(), "Download failed.", Toast.LENGTH_SHORT).show();
                    // Attempts to re-use the Task object
                    recycleTask(attachmentTask);
                    break;
                default:
                    // Otherwise, calls the super method
                    super.handleMessage(inputMessage);
                }
            }
        }
    };
}

From source file:com.nextgis.maplib.display.TMSRenderer.java

@Override
public void runDraw(final GISDisplay display) throws NullPointerException {
    final double zoom = display.getZoomLevel();

    GeoEnvelope env = display.getBounds();
    //get tiled for zoom and bounds
    final TMSLayer tmsLayer = (TMSLayer) mLayer;
    final List<TileItem> tiles = tmsLayer.getTielsForBounds(display, env, zoom);

    int threadCount = tmsLayer.getMaxThreadCount();
    if (threadCount > 0)
        mDrawThreadPool = new ThreadPoolExecutor(threadCount, threadCount, KEEP_ALIVE_TIME,
                KEEP_ALIVE_TIME_UNIT, new LinkedBlockingQueue<Runnable>(), new RejectedExecutionHandler() {
                    @Override//  w  ww .j  a v  a 2  s  . co m
                    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                        try {
                            executor.getQueue().put(r);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            //throw new RuntimeException("Interrupted while submitting task", e);
                        }
                    }
                });

    if (null == mDrawThreadPool) {
        tmsLayer.onDrawFinished(tmsLayer.getId(), 1);
        return;
    }

    if (tiles.size() == 0) {
        tmsLayer.onDrawFinished(tmsLayer.getId(), 1);
        return;
    }

    for (int i = 0; i < tiles.size(); ++i) {
        final TileItem tile = tiles.get(i);
        mDrawThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);

                final Bitmap bmp = tmsLayer.getBitmap(tile);
                if (bmp != null) {
                    display.drawTile(bmp, tile.getPoint(), mRasterPaint);
                }

                float percent = 1;
                synchronized (mLayer) {
                    if (mDrawThreadPool.getTaskCount() > 1)
                        percent = (float) (mDrawThreadPool.getCompletedTaskCount() + 1)
                                / mDrawThreadPool.getTaskCount();
                    tmsLayer.onDrawFinished(tmsLayer.getId(), percent);
                }
                //Log.d(TAG, "percent: " + percent + " complete: " + mDrawThreadPool.getCompletedTaskCount() + " task count: " + mDrawThreadPool.getTaskCount());
            }
        });
    }
}

From source file:com.cyberway.issue.crawler.frontier.BdbFrontier.java

@Override
protected void initQueuesOfQueues() {
    if (this.controller.isCheckpointRecover()) {
        // do not setup here; take/init from deserialized frontier
        return;/*ww  w .  j  a v  a 2 s  .c om*/
    }
    // small risk of OutOfMemoryError: if 'hold-queues' is false,
    // readyClassQueues may grow in size without bound
    readyClassQueues = new LinkedBlockingQueue<String>();

    try {
        Database inactiveQueuesDb = this.controller.getBdbEnvironment().openDatabase(null, "inactiveQueues",
                StoredQueue.databaseConfig());
        inactiveQueues = new StoredQueue<String>(inactiveQueuesDb, String.class, null);
        Database retiredQueuesDb = this.controller.getBdbEnvironment().openDatabase(null, "retiredQueues",
                StoredQueue.databaseConfig());
        retiredQueues = new StoredQueue<String>(retiredQueuesDb, String.class, null);
    } catch (DatabaseException e) {
        throw new RuntimeException(e);
    }

    // small risk of OutOfMemoryError: in large crawls with many 
    // unresponsive queues, an unbounded number of snoozed queues 
    // may exist
    snoozedClassQueues = Collections.synchronizedSortedSet(new TreeSet<WorkQueue>());
}

From source file:com.microsoft.live.unittest.GetTest.java

@Override
protected void setUp() throws Exception {
    super.setUp();

    this.responseQueue = new LinkedBlockingQueue<LiveOperation>();
    this.queueingListener = new OperationQueueingListener(this.exceptionQueue, this.responseQueue);
}

From source file:com.yozio.android.YozioHelper.java

YozioHelper(YozioDataStore dataStore, YozioApiService apiService) {
    this.dataStore = dataStore;
    this.apiService = apiService;
    this.dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    // This executor MUST have pool size of 1 (1 task allowed to run at once).
    // Otherwise, we might send the same event multiple times.
    executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
}

From source file:org.apache.bookkeeper.metadata.etcd.EtcdRegistrationTest.java

private void testWatchBookies(boolean readonly) throws Exception {
    LinkedBlockingQueue<Versioned<Set<BookieSocketAddress>>> writableChanges = new LinkedBlockingQueue<>();
    LinkedBlockingQueue<Versioned<Set<BookieSocketAddress>>> readonlyChanges = new LinkedBlockingQueue<>();
    result(regClient.watchReadOnlyBookies(newRegistrationListener(readonlyChanges)));
    result(regClient.watchWritableBookies(newRegistrationListener(writableChanges)));
    Versioned<Set<BookieSocketAddress>> versionedBookies = writableChanges.take();
    assertTrue(versionedBookies.getValue().isEmpty());
    versionedBookies = readonlyChanges.take();
    assertTrue(versionedBookies.getValue().isEmpty());

    final int numBookies = 3;
    final List<EtcdRegistrationManager> bookies = createNumBookies(readonly, numBookies, scope, 1);

    LinkedBlockingQueue<Versioned<Set<BookieSocketAddress>>> changes;
    if (readonly) {
        changes = readonlyChanges;/*from   w w  w. ja  v  a2s.com*/
    } else {
        changes = writableChanges;
    }

    Version preVersion = new LongVersion(-1);
    Set<BookieSocketAddress> expectedBookies = new HashSet<>();
    for (int i = 0; i < numBookies; i++) {
        BookieSocketAddress address = new BookieSocketAddress(newBookie(i));
        expectedBookies.add(address);

        versionedBookies = changes.take();
        Version curVersion = versionedBookies.getVersion();
        assertEquals(Occurred.AFTER, curVersion.compare(preVersion));
        assertEquals(expectedBookies, versionedBookies.getValue());
        preVersion = curVersion;
    }

    bookies.forEach(EtcdRegistrationManager::close);
    for (int i = 0; i < numBookies; i++) {
        versionedBookies = changes.take();
        Version curVersion = versionedBookies.getVersion();
        assertEquals(Occurred.AFTER, curVersion.compare(preVersion));
        assertEquals(numBookies - i - 1, versionedBookies.getValue().size());
        preVersion = curVersion;
    }
    if (readonly) {
        assertEquals(0, writableChanges.size());
    } else {
        assertEquals(0, readonlyChanges.size());
    }
}

From source file:org.opencron.server.service.ExecuteService.java

/**
 * ? ???//from  w w  w. java  2s  .c  om
 */
private boolean executeFlowJob(JobVo job) {
    if (!checkJobPermission(job.getAgentId(), job.getUserId()))
        return false;

    final long groupId = System.nanoTime() + Math.abs(new Random().nextInt());//??Id
    final Queue<JobVo> jobQueue = new LinkedBlockingQueue<JobVo>();
    jobQueue.add(job);
    jobQueue.addAll(job.getChildren());
    RunModel runModel = RunModel.getRunModel(job.getRunModel());
    switch (runModel) {
    case SEQUENCE:
        return executeSequenceJob(groupId, jobQueue);//
    case SAMETIME:
        return executeSameTimeJob(groupId, jobQueue);//
    default:
        return false;
    }
}

From source file:com.applozic.mobicomkit.api.attachment.AttachmentManager.java

/**
 * Constructs the work queues and thread pools used to download and decode images.
 *///from  w  w w .j  a v a 2s  . com
private AttachmentManager() {

    attachmentInProgress = new ArrayList<String>();
    attachmentTaskList = new ArrayList<AttachmentTask>();
    /*
     * Creates a work queue for the pool of Thread objects used for downloading, using a linked
     * list queue that blocks when the queue is empty.
     */
    mDownloadWorkQueue = new LinkedBlockingQueue<Runnable>();

    /*
     * Creates a work queue for the pool of Thread objects used for decoding, using a linked
     * list queue that blocks when the queue is empty.
     */
    mDecodeWorkQueue = new LinkedBlockingQueue<Runnable>();

    /*
     * Creates a work queue for the set of of task objects that control downloading and
     * decoding, using a linked list queue that blocks when the queue is empty.
     */
    mPhotoTaskWorkQueue = new LinkedBlockingQueue<AttachmentTask>();

    /*
     * Creates a new pool of Thread objects for the download work queue
     */
    mDownloadThreadPool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_TIME,
            KEEP_ALIVE_TIME_UNIT, mDownloadWorkQueue);

    /*
     * Creates a new pool of Thread objects for the decoding work queue
     */
    mDecodeThreadPool = new ThreadPoolExecutor(NUMBER_OF_CORES, NUMBER_OF_CORES, KEEP_ALIVE_TIME,
            KEEP_ALIVE_TIME_UNIT, mDecodeWorkQueue);

    // Instantiates a new cache based on the cache size estimate
    mPhotoCache = new LruCache<String, Bitmap>(IMAGE_CACHE_SIZE) {

        //            /*
        //             * This overrides the default sizeOf() implementation to return the
        //             * correct size of each cache entry.
        //             */
        //
        //            @Override
        //            protected int sizeOf(String paramURL, Bitmap image) {
        //                return image.getByteCount();
        //            }
    };
    /*
     * Instantiates a new anonymous Handler object and defines its
     * handleMessage() method. The Handler *must* run on the UI thread, because it moves photo
     * Bitmaps from the PhotoTask object to the View object.
     * To force the Handler to run on the UI thread, it's defined as part of the PhotoManager
     * constructor. The constructor is invoked when the class is first referenced, and that
     * happens when the View invokes startDownload. Since the View runs on the UI Thread, so
     * does the constructor and the Handler.
     */
    mHandler = new Handler(Looper.getMainLooper()) {

        /*
         * handleMessage() defines the operations to perform when the
         * Handler receives a new Message to process.
         */
        @Override
        public void handleMessage(Message inputMessage) {

            // Gets the image task from the incoming Message object.
            AttachmentTask attachmentTask = (AttachmentTask) inputMessage.obj;

            // Sets an PhotoView that's a weak reference to the
            // input ImageView
            AttachmentView localView = attachmentTask.getPhotoView();

            // If this input view isn't null
            if (localView != null) {

                /*
                 * Gets the URL of the *weak reference* to the input
                 * ImageView. The weak reference won't have changed, even if
                 * the input ImageView has.
                 */
                // URL localURL = localView.getLocation();

                /*
                 * Compares the URL of the input ImageView to the URL of the
                 * weak reference. Only updates the bitmap in the ImageView
                 * if this particular Thread is supposed to be serving the
                 * ImageView.
                 */
                //if (attachmentTask.getImageURL() == localURL)

                /*
                 * Chooses the action to take, based on the incoming message
                 *
                 */
                //TODO: show the status properly based on message status ...
                switch (inputMessage.what) {

                case DOWNLOAD_STARTED:
                    localView.getMessage().setAttDownloadInProgress(true);
                    localView.getProressBar().setVisibility(View.VISIBLE);
                    break;
                case DOWNLOAD_COMPLETE:
                    localView.getProressBar().setProgress(70);
                    localView.getMessage().setAttDownloadInProgress(false);
                    break;
                case DECODE_STARTED:
                    localView.getProressBar().setVisibility(View.VISIBLE);
                    localView.getProressBar().setProgress(90);
                    break;
                /*
                 * The decoding is done, so this sets the
                 * ImageView's bitmap to the bitmap in the
                 * incoming message
                 */
                case TASK_COMPLETE:

                    if (localView.getDownloadProgressLayout() != null
                            && !localView.getMessage().isAttachmentUploadInProgress()) {
                        localView.getDownloadProgressLayout().setVisibility(View.GONE);
                    } else if (localView.getProressBar() != null) {
                        localView.getProressBar().setVisibility(View.GONE);
                    }
                    BroadcastService.sendMessageUpdateBroadcast(localView.getContext(),
                            BroadcastService.INTENT_ACTIONS.MESSAGE_ATTACHMENT_DOWNLOAD_DONE.toString(),
                            localView.getMessage());
                    localView.setImageBitmap(attachmentTask.getImage());
                    recycleTask(attachmentTask);
                    break;
                // The download failed, sets the background color to dark red
                case DOWNLOAD_FAILED:
                    //localView.setStatusResource(R.drawable.imagedownloadfailed);
                    localView.getProressBar().setProgress(0);
                    localView.getMessage().setAttDownloadInProgress(false);
                    localView.getDownloadProgressLayout().setVisibility(View.GONE);
                    localView.setVisibility(View.INVISIBLE);
                    localView.cancelDownload();
                    BroadcastService.sendMessageUpdateBroadcast(localView.getContext(),
                            BroadcastService.INTENT_ACTIONS.MESSAGE_ATTACHMENT_DOWNLOAD_FAILD.toString(),
                            localView.getMessage());
                    Toast.makeText(localView.getContext(), "Download failed.", Toast.LENGTH_SHORT).show();
                    // Attempts to re-use the Task object
                    recycleTask(attachmentTask);
                    break;
                default:
                    // Otherwise, calls the super method
                    super.handleMessage(inputMessage);
                }
            }
        }
    };
}

From source file:it.drwolf.ridire.session.async.JobMapperMonitor.java

private void create() {
    int transactionTimeoutSeconds = 240;
    try {//from w w  w  .  ja v  a2 s  . c  om
        ((javax.transaction.UserTransaction) org.jboss.seam.transaction.Transaction.instance())
                .setTransactionTimeout(transactionTimeoutSeconds);
    } catch (SystemException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Protocol.registerProtocol("https", new Protocol("https", new EasySSLProtocolSocketFactory(), 8443));
    this.httpClient = new HttpClient();
    this.httpClient.getParams().setAuthenticationPreemptive(true);
    this.mainUserTx = (UserTransaction) org.jboss.seam.Component
            .getInstance("org.jboss.seam.transaction.transaction", ScopeType.APPLICATION);
    Credentials defaultcreds = null;
    int jobsToBeProcessed = 4;
    try {
        this.mainUserTx.setTransactionTimeout(10 * 10 * 60);
        // 10 mins
        this.mainUserTx.begin();
        this.eventEntityManager.joinTransaction();
        defaultcreds = new UsernamePasswordCredentials("admin", this.eventEntityManager
                .find(CommandParameter.class, CommandParameter.HERITRIX_ADMINPW_KEY).getCommandValue());
        jobsToBeProcessed = Integer.parseInt(this.eventEntityManager
                .find(Parameter.class, Parameter.JOBS_TO_BE_PROCESSED.getKey()).getValue());
        JobMapperMonitor.JOBSDIR = this.eventEntityManager.find(Parameter.class, Parameter.JOBS_DIR.getKey())
                .getValue();
        this.flagBearer.setHostname(
                this.eventEntityManager.find(Parameter.class, Parameter.HOSTNAME.getKey()).getValue());
        this.eventEntityManager.flush();
        this.mainUserTx.commit();
    } catch (Exception e) {
        e.printStackTrace();
    }
    this.httpClient.getState().setCredentials(
            new AuthScope(AuthScope.ANY_SCHEME, AuthScope.ANY_PORT, AuthScope.ANY_REALM), defaultcreds);
    Logger httpClientlogger = Logger.getLogger(this.httpClient.getClass());
    httpClientlogger.setLevel(Level.ERROR);
    Logger authChallengeProcessorLogger = Logger.getLogger(AuthChallengeProcessor.class);
    authChallengeProcessorLogger.setLevel(Level.ERROR);
    Logger httpMethodBaseLogger = Logger.getLogger(HttpMethodBase.class);
    httpMethodBaseLogger.setLevel(Level.ERROR);
    this.highPoolThreadFactory = new PoolThreadFactory(JobMapperMonitor.THREADS_PRIORITY);
    this.threadPool = new ThreadPoolExecutor(jobsToBeProcessed, jobsToBeProcessed, 100, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(), this.highPoolThreadFactory);
}

From source file:com.datatorrent.lib.dedup.Deduper.java

public Deduper() {
    waitingEvents = Maps.newHashMap();//from   w  w  w. j a  v  a  2 s .c  o m
    partitionKeys = Sets.newHashSet(0);
    partitionMask = 0;

    fetchedBuckets = new LinkedBlockingQueue<Bucket<INPUT>>();
    counters = new BasicCounters<MutableLong>(MutableLong.class);
}