Example usage for java.util.concurrent LinkedBlockingQueue take

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

Introduction

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

Prototype

public E take() throws InterruptedException 

Source Link

Usage

From source file:org.commoncrawl.util.S3ArcFileReader.java

private Thread startDecoderThread(final int threadIdx,
        final LinkedBlockingQueue<QueuedBufferItem> attachedQueue) {

    Thread decoderThread = new Thread(new Runnable() {

        public void run() {
            //LOG.info("Decoder Thread Start");
            while (true) {

                try {
                    QueuedBufferItem bufferItem = attachedQueue.take();

                    if (bufferItem._key == null) {
                        //this is our signal to shutdown and exit
                        //LOG.info("Decoder Thread received shutdown signal");
                        _items.add(new QueuedArcFileItem(null, null));
                        return;
                    }//from   w ww .j  av a 2  s. co m

                    // LOG.info("Decoder Thread Got Buffer Item for Key:" + bufferItem._key);
                    StreamingArcFileReader decoder = null;

                    synchronized (_decodeStateMaps[threadIdx]) {
                        decoder = _decodeStateMaps[threadIdx].get(bufferItem._key);
                    }
                    if (decoder == null) {
                        LOG.error("No Decode State found for Key:" + bufferItem._key);
                    } else {
                        if (bufferItem._buffer != null) {
                            decoder.available(bufferItem._buffer);
                        } else {
                            //LOG.info("Calling Decoder Finished For Key:" + bufferItem._key);
                            decoder.finished();
                        }

                        try {

                            int itemsProduced = 0;

                            StreamingArcFileReader.TriStateResult result = decoder.hasMoreItems();

                            while (result == StreamingArcFileReader.TriStateResult.MoreItems) {
                                ArcFileItem item = decoder.getNextItem();
                                if (item != null) {
                                    itemsProduced++;
                                    //LOG.info("Decoder Got Item:" + item.getUri() + " For Key:" + bufferItem._key);
                                    _items.offer(new QueuedArcFileItem(bufferItem._key, item));
                                } else {
                                    break;
                                }
                            }

                            // LOG.info("Stream:" + bufferItem._key + " Bytes:" + bufferBytes + " Items:" + itemsProduced);

                            if (result == StreamingArcFileReader.TriStateResult.NoMoreItems) {
                                decoder.resetState();
                                synchronized (_decodeStateMaps[threadIdx]) {
                                    _decodeStateMaps[threadIdx].remove(bufferItem._key);
                                }
                            }
                        } catch (IOException e) {
                            decoder.resetState();
                            synchronized (_decodeStateMaps[threadIdx]) {
                                _decodeStateMaps[threadIdx].remove(bufferItem._key);
                            }
                            LOG.error(StringUtils.stringifyException(e));
                        }
                    }
                } catch (InterruptedException e) {
                }
            }
        }

    });

    decoderThread.start();

    return decoderThread;
}