List of usage examples for java.util.concurrent ArrayBlockingQueue put
public void put(E e) throws InterruptedException
From source file:Main.java
public static void main(String[] argv) throws Exception { List<Integer> list = new ArrayList<Integer>(); int capacity = 100; ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(capacity); queue.put(0); queue.put(1);//from w ww . jav a2 s .co m queue.put(2); queue.drainTo(list, 3); System.out.println(queue); System.out.println(list); }
From source file:Main.java
public static void main(String[] argv) throws Exception { List<Integer> list = new ArrayList<Integer>(); int capacity = 100; ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(capacity); queue.put(0); queue.put(1);/*from w ww .j av a 2 s . c om*/ queue.put(2); queue.drainTo(list); System.out.println(queue); System.out.println(list); }
From source file:Main.java
public static void main(String[] argv) throws Exception { List<Integer> list = Arrays.asList(new Integer[] { 0, 1, 2, 3 }); int capacity = 100; ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(capacity, true, list); queue.put(0); queue.put(1);/*from w ww . ja v a 2 s . c om*/ queue.put(2); System.out.println(queue); System.out.println(list); }
From source file:Main.java
public static void main(String[] argv) throws Exception { int capacity = 10; ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(capacity); int numWorkers = 2; Worker[] workers = new Worker[numWorkers]; for (int i = 0; i < workers.length; i++) { workers[i] = new Worker(queue); workers[i].start();/*from www . j a va 2s. c om*/ } for (int i = 0; i < 100; i++) { queue.put(i); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { int capacity = 10; ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(capacity, false); int numWorkers = 2; Worker[] workers = new Worker[numWorkers]; for (int i = 0; i < workers.length; i++) { workers[i] = new Worker(queue); workers[i].start();//from www . java 2 s . co m } for (int i = 0; i < 100; i++) { queue.put(i); } }
From source file:code.google.nfs.rpc.client.AbstractClient.java
/** * receive response// w w w . j a v a2 s.co m */ public void putResponse(ResponseWrapper wrapper) throws Exception { if (!responses.containsKey(wrapper.getRequestId())) { LOGGER.warn("give up the response,request id is:" + wrapper.getRequestId() + ",maybe because timeout!"); return; } try { ArrayBlockingQueue<Object> queue = responses.get(wrapper.getRequestId()); if (queue != null) { queue.put(wrapper); } else { LOGGER.warn( "give up the response,request id is:" + wrapper.getRequestId() + ",because queue is null"); } } catch (InterruptedException e) { LOGGER.error("put response error,request id is:" + wrapper.getRequestId(), e); } }
From source file:code.google.nfs.rpc.client.AbstractClient.java
/** * receive responses/*from w w w . jav a2s.c o m*/ */ public void putResponses(List<ResponseWrapper> wrappers) throws Exception { for (ResponseWrapper wrapper : wrappers) { if (!responses.containsKey(wrapper.getRequestId())) { LOGGER.warn( "give up the response,request id is:" + wrapper.getRequestId() + ",maybe because timeout!"); continue; } try { ArrayBlockingQueue<Object> queue = responses.get(wrapper.getRequestId()); if (queue != null) { queue.put(wrappers); break; } else { LOGGER.warn("give up the response,request id is:" + wrapper.getRequestId() + ",because queue is null"); } } catch (InterruptedException e) { LOGGER.error("put response error,request id is:" + wrapper.getRequestId(), e); } } }
From source file:com.taobao.common.tfs.comm.TfsClient.java
protected void putResponse(Integer requestId, Object response) throws TfsException { if (responses.containsKey(requestId)) { try {/* w ww . j a v a 2 s.c om*/ ArrayBlockingQueue<Object> queue = responses.get(requestId); if (queue != null) { queue.put(response); if (isDebugEnabled) { log.debug("put response [" + requestId + "],time is:" + System.currentTimeMillis()); } } else if (isDebugEnabled) { log.debug("give up the response, maybe because timeout, requestId is:" + requestId); } } catch (InterruptedException e) { throw new TfsException("put response error", e); } } else { log.warn("give up the response, invalid request Id: " + requestId); } }
From source file:com.taobao.tair.comm.TairClient.java
protected void putResponse(Integer requestId, Object response) throws TairClientException { if (responses.containsKey(requestId)) { try {//from w w w .ja v a2s .co m ArrayBlockingQueue<Object> queue = responses.get(requestId); if (queue != null) { queue.put(response); if (isDebugEnabled) { LOGGER.debug("put response [" + requestId + "],time is:" + System.currentTimeMillis()); } } else if (isDebugEnabled) { LOGGER.debug("give up the response,maybe because timeout,requestId is:" + requestId); } } catch (InterruptedException e) { throw new TairClientException("put response error", e); } } else { if (isDebugEnabled) LOGGER.debug("give up the response,maybe because timeout,requestId is:" + requestId); } }
From source file:gsn.vsensor.RVirtualSensor.java
public void dataAvailable(String inputStreamName, StreamElement streamElement) { ArrayBlockingQueue<StreamElement> circularBuffer = circularBuffers.get(inputStreamName); // Get the circular buffer that matches the input stream. Create a new one // if none exists if (circularBuffer == null) { circularBuffer = new ArrayBlockingQueue<StreamElement>(windowSize); circularBuffers.put(inputStreamName, circularBuffer); }//from w w w.j a v a 2 s .c o m try { circularBuffer.put(streamElement); logger.debug( "Window for " + inputStreamName + " contains: " + circularBuffer.size() + " of " + windowSize); if (circularBuffer.size() == windowSize) { logger.info("Window for " + inputStreamName + " contains: " + circularBuffer.size() + " of " + windowSize); // Connect to Rserve and assign global variables try { rc = new RConnection(params.get(SERVER), Integer.parseInt(params.get(PORT))); logger.info("Connected to R server " + params.get(SERVER) + ":" + params.get(PORT)); String[] fieldname = streamElement.getFieldNames(); logger.info("Sending " + fieldname.length + " data attributes to R server."); // Assign R vector variables prior the script for (int n = 0; n < fieldname.length; n++) { // Build the window double[] values = new double[windowSize]; StreamElement elt = null; // convert the circular buffer to an array Object[] elts = circularBuffer.toArray(); for (int i = 0; i < elts.length; i++) { elt = (StreamElement) elts[i]; values[i] = ((Number) elt.getData()[n]).doubleValue(); // } // assign vectors as R variables rc.assign("gsn_" + fieldname[n].toLowerCase(), values); } logger.info("Done."); // read the R script // open the script file every time we do some processing (this can be // improved). File file = new File(params.get(SCRIPT).toString()); script = FileUtils.readFileToString(file, "UTF-8"); logger.info("Sending R script."); // evaluate the R script rc.voidEval(script); logger.info("Done."); // get the output timestamp logger.info("Performing computation in R server (please wait)."); // collect vector values after computation DataField[] outStructure = null; outStructure = getVirtualSensorConfiguration().getOutputStructure(); String[] plotFieldName = new String[outStructure.length]; Byte[] plotFieldType = new Byte[outStructure.length]; for (int w = 0; w < outStructure.length; w++) { plotFieldName[w] = outStructure[w].getName(); plotFieldType[w] = outStructure[w].getDataTypeID(); } Serializable[] outputData = null; StreamElement se = null; Byte[] fieldType = streamElement.getFieldTypes(); // check if we have defined more attributes in the output structure if (outStructure.length > fieldname.length) { outputData = new Serializable[outStructure.length]; } else { outputData = new Serializable[fieldname.length]; } for (int n = 0; n < fieldname.length; n++) { // evaluate/get attribute data from R server xp = rc.parseAndEval(fieldname[n].toLowerCase()); if (fieldType[n] == DataTypes.DOUBLE) { double[] b1 = xp.asDoubles(); outputData[n] = b1[b1.length - 1]; } if (fieldType[n] == DataTypes.INTEGER) { int[] b1 = xp.asIntegers(); outputData[n] = b1[b1.length - 1]; } if (fieldType[n] == DataTypes.BIGINT) { int[] b1 = xp.asIntegers(); outputData[n] = (long) b1[b1.length - 1]; } } int len1 = outStructure.length; int len2 = fieldname.length; // check if we have defined more attributes in the output structure if (len1 > len2) { if (stype.equals("plot")) { xp = rc.parseAndEval("gsn_plot"); outputData[len2] = xp.asBytes(); se = new StreamElement(plotFieldName, plotFieldType, outputData); } } else { se = new StreamElement(fieldname, fieldType, outputData); } logger.info("Computation finished."); dataProduced(se); logger.debug("Stream published: " + se.toString().toLowerCase()); // Close connection to R server rc.close(); logger.info("Connection to R server closed."); } catch (Exception e) { logger.warn(e.getMessage()); // Close connection to R server logger.info("Connection to R server closed."); rc.close(); } // Remove step size elements from the beginning of the buffer for (int i = 0; i < stepSize; i++) { try { circularBuffer.take(); } catch (InterruptedException e) { logger.warn(e.getMessage(), e); } } } // end if if for window } catch (InterruptedException e) { logger.warn(e.getMessage(), e); } }