public long getNextSequence(TypeDescriptor typeDescriptor, Session session) { // check if there is an id in the cache ConcurrentLinkedQueue cachedIds = (ConcurrentLinkedQueue) this.typeDescriptorToIdCache.get(typeDescriptor); if (null == cachedIds) { typeDescriptorToIdCache.putIfAbsent(typeDescriptor, new ConcurrentLinkedQueue()); cachedIds = (ConcurrentLinkedQueue) this.typeDescriptorToIdCache.get(typeDescriptor); } Number cachedId = (Number) cachedIds.poll(); if (cachedId == null) { synchronized (cachedIds) { cachedId = (Number) cachedIds.poll(); if (cachedId == null) { List newIds = this.getNextSequenceImpl(typeDescriptor, session); Assert.condition(0 < newIds.size()); // reserve first for own use cachedId = (Number) newIds.remove(0); cachedIds.addAll(newIds); } } } if (trace.isDebugEnabled()) { trace.debug("returning unique ID: " + cachedId.longValue()); } return cachedId.longValue(); }
@SuppressWarnings("unchecked") @Scheduled(fixedRate = 1000) public void processDATEXIIUpdateXML() { working = true; if (logger.isTraceEnabled()) { logger.trace("Polling for messages"); } String xml; synchronized (messageQueue) { xml = messageQueue.poll(); } while (xml != null) { try { byte[] bytes = xml.getBytes(); ByteArrayInputStream bais = new ByteArrayInputStream(bytes); XMLInputFactory xif = XMLInputFactory.newFactory(); XMLStreamReader xsr = null; boolean foundBody = false; xsr = xif.createXMLStreamReader(bais); while (!foundBody && xsr.hasNext()) { if (xsr.next() == XMLStreamConstants.START_ELEMENT && "http://schemas.xmlsoap.org/soap/envelope/".equals(xsr.getNamespaceURI()) && "Body".equals(xsr.getLocalName())) { foundBody = true; xsr.next(); } } JAXBElement<D2LogicalModel> root; Unmarshaller unmarshaller = jaxbService.createUnmarshaller(); if (foundBody) { root = (JAXBElement<D2LogicalModel>) unmarshaller.unmarshal(xsr); } else { InputStream inputStream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)); root = (JAXBElement<D2LogicalModel>) unmarshaller.unmarshal(inputStream); } D2LogicalModel d2lm = root.getValue(); FeedType feedType = getFeedType(d2lm); DATEXIIProcessService datexiiProcessService = datexiiProcessServiceFactory.getDATEXIIProcessService(feedType); if (datexiiProcessService != null) { datexiiProcessService.processMessage(d2lm); } } catch (JAXBException e) { logger.error("Failed to process XML", e); } catch (XMLStreamException e) { logger.error("XMLStreamException", e); } synchronized (messageQueue) { xml = messageQueue.poll(); } } working = false; }
@Override public void run() { while (active) { RtmpPacket rtmpPacket = writeQueue.poll(); // Write all queued RTMP packets while (rtmpPacket != null) { try { final ChunkStreamInfo chunkStreamInfo = rtmpSessionInfo.getChunkStreamInfo(rtmpPacket.getHeader().getChunkStreamId()); chunkStreamInfo.setPrevHeaderTx(rtmpPacket.getHeader()); L.d("WriteThread: writing packet: " + rtmpPacket); rtmpPacket.writeTo(out, rtmpSessionInfo.getChunkSize(), chunkStreamInfo); System.out.println( "writethread wrote packet: " + rtmpPacket + ", size: " + rtmpPacket.getHeader().getPacketLength()); if (rtmpPacket instanceof Command) { rtmpSessionInfo.addInvokedCommand( ((Command) rtmpPacket).getTransactionId(), ((Command) rtmpPacket).getCommandName()); } } catch (IOException ex) { L.e("WriteThread: Caught IOException during write loop, shutting down", ex); active = false; } rtmpPacket = writeQueue.poll(); } try { out.flush(); } catch (IOException ex) { L.e("WriteThread: Caught IOException while flushing stream, shutting down", ex); active = false; continue; } // Wait for next command synchronized (lock) { try { lock.wait(); } catch (InterruptedException ex) { L.w("WriteThread: Interrupted", ex); } } } // Close outputstream try { out.close(); } catch (Exception ex) { L.w("WriteThread: Failed to close outputstream", ex); } L.d("WriteThread: exiting"); if (threadController != null) { threadController.threadHasExited(this); } }
@Override public void run() { for (int i = 0; i < 5000; i++) { list.poll(); list.poll(); } }
/** Gets a client from the pool. If the pool is empty a new client is created. */ public MetaStoreClient getClient() { // The MetaStoreClient c'tor relies on knowing the Hadoop version by asking // org.apache.hadoop.util.VersionInfo. The VersionInfo class relies on opening // the 'common-version-info.properties' file as a resource from hadoop-common*.jar // using the Thread's context classloader. If necessary, set the Thread's context // classloader, otherwise VersionInfo will fail in it's c'tor. if (Thread.currentThread().getContextClassLoader() == null) { Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader()); } MetaStoreClient client = clientPool_.poll(); // The pool was empty so create a new client and return that. // Serialize client creation to defend against possible race conditions accessing // local Kerberos state (see IMPALA-825). synchronized (this) { try { Thread.sleep(clientCreationDelayMs_); } catch (InterruptedException e) { /* ignore */ } if (client == null) { client = new MetaStoreClient(hiveConf_); } else { // TODO: Due to Hive Metastore bugs, there is leftover state from previous client // connections so we are unable to reuse the same connection. For now simply // reconnect each time. One possible culprit is HIVE-5181. client = new MetaStoreClient(hiveConf_); } } client.markInUse(); return client; }
/** Flush any caches to the database TODO */ private void _flush() { // periodically update protections in the database if a non-critical change was made if (protectionUpdateQueue.size() > 0) { Connection connection = lwc.getPhysicalDatabase().getConnection(); Protection protection = null; try { connection.setAutoCommit(false); } catch (SQLException e) { e.printStackTrace(); } /* * Loop through */ while ((protection = protectionUpdateQueue.poll()) != null) { protection.saveNow(); } /* * Commit */ try { connection.commit(); connection.setAutoCommit(true); } catch (SQLException e) { e.printStackTrace(); } } flush = false; lastUpdate = System.currentTimeMillis(); }
/** * Releases any resources held by the writer. * * @param shutdownWrapped If the wrapped writer should be closed as well. */ public void shutdown(boolean shutdownWrapped) { stopRequested.set(true); // Wait for publisher thread to terminate while (writerThread != null && writerThread.isAlive()) { try { // Interrupt the thread if its blocking writerThread.interrupt(); writerThread.join(); } catch (InterruptedException ex) { // Ignore; we gotta wait.. } } // The writer writerThread SHOULD have drained the queue. // If not, handle outstanding requests ourselves, // and push them to the writer. while (!queue.isEmpty()) { String message = queue.poll(); writer.writeRecord(message); } // Shutdown the wrapped writer. if (shutdownWrapped && writer != null) writer.shutdown(); DirectoryServer.deregisterShutdownListener(this); }
/** * Retrieves the first available object in the pool or returns null if none are available. * * @return T * @throws Exception */ public T request() throws Exception { T t = queue.poll(); if (t != null) { if (generator != null) generator.enable(t); } return t; }
private OctreeIterator borrowIterator() { OctreeIterator itr = iteratorQueue.poll(); if (itr == null) { System.err.println("Octree iterator starved"); } return itr; }
/** Selects on sockets and informs their Communicator when there is something to do. */ public void communicate(int timeout) { try { selector.select(timeout); } catch (IOException e) { // Not really sure why/when this happens yet return; } Iterator<SelectionKey> keys = selector.selectedKeys().iterator(); while (keys.hasNext()) { SelectionKey key = keys.next(); keys.remove(); if (!key.isValid()) continue; // WHY Communicator communicator = (Communicator) key.attachment(); if (key.isReadable()) communicator.onReadable(); if (key.isWritable()) communicator.onWritable(); if (key.isAcceptable()) communicator.onAcceptable(); } // Go through the queue and handle each communicator while (!queue.isEmpty()) { Communicator c = queue.poll(); c.onMemo(); } }
public TileGenerator get() { TileGenerator i = mPool.poll(); if (i == null) { return new TileGenerator(); } return i; }
protected ServerMessage dequeue() { ServerMessage m = mq.poll(); if (m != null) { queueOut.addAndGet(1); } return m; }
private void consumeBatchToCursor(long cursor, EventHandler<Object> handler) { for (long curr = _consumer.get() + 1; curr <= cursor; curr++) { try { MutableObject mo = _buffer.get(curr); Object o = mo.o; mo.setObject(null); if (o == FLUSH_CACHE) { Object c = null; while (true) { c = _cache.poll(); if (c == null) break; else handler.onEvent(c, curr, true); } } else if (o == INTERRUPT) { throw new InterruptedException("Disruptor processing interrupted"); } else { handler.onEvent(o, curr, curr == cursor); } } catch (Exception e) { throw new RuntimeException(e); } } // TODO: only set this if the consumer cursor has changed? _consumer.set(cursor); }
/** * Allows a party to occupy the room based on the queued parties. The party that has all their * members there first will be allowed to occupy. */ private synchronized void occupyRoom() { Party party = partyQueue.poll(); if (party == null) { return; } String meetingFormat = ""; if (party.getPartyLeader().getManager() == null) { meetingFormat = "manager's project"; } else { meetingFormat = "team (N/A)-standup meeting"; } for (AbstractWorker worker : party.getAttendees()) { worker.beginTeamMeeting(meetingFormat); } partyEntersRoom(party); try { Thread.sleep(Const.TL_AND_MGR_MTG.value() * Const.TIMESTEP.value()); } catch (InterruptedException e) { System.err.println("InterruptedException"); } partyLeavesRoom(party); }
/** * Convert the items in the list to a JsonUtil array of JsonUtil objects, and remove these objects * from the list. Currently only String fields are supported. * * @param list The list as the source of objects to be converted to JsonUtil array. * @param cnt The number of objects to be converted, and deleted from the list. e.g. cnt = 10 * means list[0] to list[9] will be converted and deleted. */ public static String linkedList2Json(ConcurrentLinkedQueue list, int cnt) { StringBuilder sb = new StringBuilder(); // List sub = list..subList(0, cnt); LinkedList sub = new LinkedList(); for (int i = 0; i < cnt; i++) sub.add(list.poll()); Field[] fields = null; Class cls = null; // create a json array sb.append("{\"objects\":["); boolean firstObject = true; for (Object obj : sub) { if (firstObject) { sb.append("{"); firstObject = false; } else { sb.append(", {"); } if (fields == null) { fields = obj.getClass().getFields(); cls = obj.getClass(); } // do sth to retrieve each field value -> json property of json object // add to json array for (int i = 0; i < fields.length; i++) { Field f = fields[i]; jsonFromField(sb, obj, i, f); } sb.append("}"); } sb.append("]}"); sub.clear(); return sb.toString(); }
@Override public void run() { if (operatori == null) { operatori = new Operators(); } Evaluator evaluator = ((IEvaluatorProvider) Thread.currentThread()).getEvaluator(); Integer brojZaNapraviti = null; while (true) { brojZaNapraviti = ulazniRedTotalParalel.poll(); if (brojZaNapraviti != null) { for (int i = 0; i < brojZaNapraviti; i++) { Solution p1 = operatori.findParent(populacija, Constants.tournament); Solution p2 = operatori.findParent(populacija, Constants.tournament); Solution child = operatori.createChild(p1, p2); evaluator.evaluate(child); izlazniRedTotalParalel.add(child); } } else { break; } } }
/** Borrow a buffer from the pool. If none in the pool, create a new one. */ public ByteBuffer borrowBuffer() { ByteBuffer buffer = pool.poll(); if (buffer == null) { buffer = ByteBuffer.allocateDirect(bufferSize + guard.length); initializeGuard(buffer); if (logger.isDetailEnabled()) logger.detail( "FixedByteBufferPoolImpl for " + name + " got new buffer: position " + buffer.position() + " capacity " + buffer.capacity() + " limit " + buffer.limit()); } else { if (logger.isDetailEnabled()) logger.detail( "FixedByteBufferPoolImpl for " + name + " got used buffer: position " + buffer.position() + " capacity " + buffer.capacity() + " limit " + buffer.limit()); } buffer.clear(); return buffer; }
protected boolean checkOtherQueues() { ParsedFileResult fileResult = null; if ((fileResult = fileResultQueue.poll()) != null) { processFileResult(fileResult); } return fileResult != null; }
public void run() { WikipediaDocument doc; Map<INDEXFIELD, Tokenizer> tknizerMap; while (!Thread.interrupted()) { doc = queue.poll(); if (doc == null) { try { numTries++; if (numTries > 5) { sleepTime *= 2; numTries = 0; } Thread.sleep(sleepTime); } catch (InterruptedException e) { } } else { if (numTries > 0) numTries--; tknizerMap = initMap(properties); pool.submit(new DocumentTransformer(tknizerMap, doc)); } } }
@Override public CheckSummedBuffer poll(long requiredBlocks) { CheckSummedBuffer ret = null; // busy wait if resource if not available while ((ret = buffers.poll()) == null) ; return ret; }
public String getNextInboundMessage() { String result = null; if (!inboundCommandQueue.isEmpty()) { result = inboundCommandQueue.poll(); } return result; }
void sendPartitionInfos() { // check if the ExecutionVertex has already been archived and thus cleared the // partial partition infos queue if (partialInputChannelDeploymentDescriptors != null && !partialInputChannelDeploymentDescriptors.isEmpty()) { PartialInputChannelDeploymentDescriptor partialInputChannelDeploymentDescriptor; List<IntermediateDataSetID> resultIDs = new ArrayList<IntermediateDataSetID>(); List<InputChannelDeploymentDescriptor> inputChannelDeploymentDescriptors = new ArrayList<InputChannelDeploymentDescriptor>(); while ((partialInputChannelDeploymentDescriptor = partialInputChannelDeploymentDescriptors.poll()) != null) { resultIDs.add(partialInputChannelDeploymentDescriptor.getResultId()); inputChannelDeploymentDescriptors.add( partialInputChannelDeploymentDescriptor.createInputChannelDeploymentDescriptor(this)); } UpdatePartitionInfo updateTaskMessage = createUpdateTaskMultiplePartitionInfos( attemptId, resultIDs, inputChannelDeploymentDescriptors); sendUpdatePartitionInfoRpcCall(assignedResource, updateTaskMessage); } }
/** Transport connected. {@link IOTransport} calls this when a connection is established. */ public void transportConnected() { setState(STATE_READY); if (reconnectTask != null) { reconnectTask.cancel(); reconnectTask = null; } resetTimeout(); synchronized (outputBuffer) { if (transport.canSendBulk()) { ConcurrentLinkedQueue<String> outputBuffer = this.outputBuffer; this.outputBuffer = new ConcurrentLinkedQueue<String>(); try { // DEBUG String[] texts = outputBuffer.toArray(new String[outputBuffer.size()]); log.debug("Bulk start:"); for (String text : texts) { log.debug("> " + text); } log.debug("Bulk end"); // DEBUG END transport.sendBulk(texts); } catch (IOException e) { this.outputBuffer = outputBuffer; } } else { String text; while ((text = outputBuffer.poll()) != null) { sendPlain(text); } } this.keepAliveInQueue = false; } }
public void checkqueue() { if (!ismoving && !movequeue.isEmpty()) { ismoving = true; UI.instance.mainview.moveto = movequeue.poll(); movequeue.remove(0); } }
/** * Retrieves the first available object in the pool or creates a new instance if there are none * available. * * @return T */ public T get() { T t = queue.poll(); if (t == null) { t = newInstance(); } if (generator != null) generator.enable(t); return t; }
private void cacheNotification(ApnsNotification notification) { cachedNotifications.add(notification); while (cachedNotifications.size() > cacheLength) { cachedNotifications.poll(); logger.debug("Removing notification from cache " + notification); UILogger.getInstance().add("Removing notification from cache " + notification); } }
@Override public boolean offer(E e) { if (queue.size() > capacity) { queue.poll(); } queue.offer(e); return true; }
private void writeFromQueue() { WebSocketFrame frame = queue.poll(); if (frame != null) { writeBuf.put(frame.encode()); writeBuf.flip(); channel.write(writeBuf, attachment, this); } }
public ChannelBuffer poll() { ChannelBuffer channelBuffer; if ((channelBuffer = pool.poll()) == null) { channelBuffer = createObject(); } return channelBuffer; }
public TrPeerInfo getPeerForAssimilation() { if (peers.isEmpty()) { // We need to use a public peer final ArrayList<File> publicNodeIdFiles = node.getPublicNodeIdFiles(); final File pubPeerFile = publicNodeIdFiles.get(TrUtils.rand.nextInt(publicNodeIdFiles.size())); final TrPeerInfo pnii = Persistence.loadReadOnly(TrPeerInfo.class, pubPeerFile); return pnii; } else { /** * Here we use a trick to pick peers in proportion to the probability that they will be the * fastest peer */ TrPeerInfo bestPeer = null; double bestTimeEstimate = Double.MAX_VALUE; final LinearStat globalSuccessTime = new LinearStat(Integer.MAX_VALUE); globalSuccessTime.sample(1000); globalSuccessTime.sample(2000); for (final TrPeerInfo ifo : peers.values()) { if (ifo.assimilation.successTimeSqrt.total > 0) { globalSuccessTime.sample(ifo.assimilation.successTimeSqrt.mean()); } } for (final Entry<PhysicalNetworkLocation, TrPeerInfo> e : peers.entrySet()) { final double guessFailureProb = e.getValue().assimilation.successRate.getBetaRandom(); double guessSuccessTime; // If we don't have at least 2 samples, use our global success // time if (e.getValue().assimilation.successTimeSqrt.total > 2) { final double guessSuccessTimeSqrt = e.getValue().assimilation.successTimeSqrt.getNormalRandom(); guessSuccessTime = guessSuccessTimeSqrt * guessSuccessTimeSqrt; } else { final double guessSuccessTimeSqrt = globalSuccessTime.getNormalRandom(); guessSuccessTime = guessSuccessTimeSqrt * guessSuccessTimeSqrt; } double timeEstimate = guessSuccessTime + AssimilateSessionImpl.RELAY_ASSIMILATION_TIMEOUT_SECONDS * 1000l * guessFailureProb; if (lastAttemptedRelays.contains(e.getValue())) { timeEstimate *= RECENTLY_ATTEMPTED_PENALTY; } if (timeEstimate < bestTimeEstimate) { bestPeer = e.getValue(); bestTimeEstimate = timeEstimate; } } lastAttemptedRelays.add(bestPeer); while (lastAttemptedRelays.size() > 5) { lastAttemptedRelays.poll(); } return bestPeer; } }