本文整理汇总了Java中java.util.Queue类的典型用法代码示例。如果您正苦于以下问题:Java Queue类的具体用法?Java Queue怎么用?Java Queue使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Queue类属于java.util包,在下文中一共展示了Queue类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: jdkQueue
import java.util.Queue; //导入依赖的package包/类
public void jdkQueue() {
Queue<Integer> queue = new LinkedList<Integer>();
PrinterUtils.printELog("队列大小:" + queue.size());
Integer arr[] = {1, 2, 3, 4, 5};
//队列里放元素
for (int i : arr) {
queue.offer(i);
}
PrinterUtils.printELog("队列大小:" + queue.size());
//移除
while (!queue.isEmpty()) {
PrinterUtils.printELog(
"队列头部元素:" + queue.peek() +
",出队:" + queue.poll());
}
}
示例2: processUpdates
import java.util.Queue; //导入依赖的package包/类
/**
* Send update notifications to listeners
* @param updates the updates to process.
*/
protected void processUpdates(Queue<DeviceUpdate> updates) {
if (updates == null) return;
DeviceUpdate update = null;
while (null != (update = updates.poll())) {
if (logger.isTraceEnabled()) {
logger.trace("Dispatching device update: {}", update);
}
if (update.change == DeviceUpdate.Change.DELETE) {
deviceSyncManager.removeDevice(update.device);
} else {
deviceSyncManager.storeDevice(update.device);
}
List<IDeviceListener> listeners = deviceListeners.getOrderedListeners();
notifyListeners(listeners, update);
}
}
示例3: testSerialization
import java.util.Queue; //导入依赖的package包/类
/**
* A deserialized/reserialized deque has same elements in same order
*/
public void testSerialization() throws Exception {
Queue x = populatedDeque(SIZE);
Queue y = serialClone(x);
assertNotSame(y, x);
assertEquals(x.size(), y.size());
assertEquals(x.toString(), y.toString());
assertEquals(Arrays.toString(x.toArray()), Arrays.toString(y.toArray()));
assertTrue(Arrays.equals(x.toArray(), y.toArray()));
while (!x.isEmpty()) {
assertFalse(y.isEmpty());
assertEquals(x.remove(), y.remove());
}
assertTrue(y.isEmpty());
}
示例4: sendCommand
import java.util.Queue; //导入依赖的package包/类
void sendCommand(Handler<AsyncResult<Queue<byte[]>>> handler, String key, Object... params)
{
if (batches != null) {
batches.add(new BatchCommand(key, params, handler));
return;
}
switch (status) {
case Normal:
connecter.tryGetConnection(F.ofSucceededVoid(handler, conn -> conn.execute(key, params, handler)));
break;
case Once:
connecter.tryGetConnection(F.ofSucceededVoid(handler, conn -> conn.execute(key, params, F.ofSucceeded(handler, () -> {
status = Status.Unusable;
vertx.runOnContext(event -> close(F.noneHandle()));
}, queue -> queue))));
break;
case Unusable:
handler.handle(F.failedFuture(new SSDBClosedException()));
break;
}
}
示例5: removeListener
import java.util.Queue; //导入依赖的package包/类
public boolean removeListener(String channelName, EventListener msgListener) {
Queue<RedisPubSubListener<?>> listeners = channelListeners.get(channelName);
for (RedisPubSubListener<?> listener : listeners) {
if (listener instanceof PubSubMessageListener) {
if (((PubSubMessageListener)listener).getListener() == msgListener) {
removeListener(channelName, listener);
return true;
}
}
if (listener instanceof PubSubPatternMessageListener) {
if (((PubSubPatternMessageListener)listener).getListener() == msgListener) {
removeListener(channelName, listener);
return true;
}
}
}
return false;
}
示例6: validateMessage
import java.util.Queue; //导入依赖的package包/类
@ValidationExecutor
private void validateMessage(Message m, Group g) {
MessageValidator v = validators.get(g.getClientId());
if (v == null) {
if (LOG.isLoggable(WARNING))
LOG.warning("No validator for " + g.getClientId().getString());
} else {
try {
MessageContext context = v.validateMessage(m, g);
storeMessageContextAsync(m, g.getClientId(), context);
} catch (InvalidMessageException e) {
if (LOG.isLoggable(INFO))
LOG.log(INFO, e.toString(), e);
Queue<MessageId> invalidate = new LinkedList<MessageId>();
invalidate.add(m.getId());
invalidateNextMessageAsync(invalidate);
}
}
}
示例7: createConnectedComponent
import java.util.Queue; //导入依赖的package包/类
private static List<Integer> createConnectedComponent(HashMap<Integer, List<Integer>> graph, int root) {
Queue<Integer> q = new ArrayDeque<>(); // next nodes to visit
List<Integer> visited = new ArrayList<>(); // connected component so far
q.add(root); // init queue
while (!q.isEmpty()) {
Integer poll = q.poll(); // get next node
if (!visited.contains(poll)) { // if it's not already visited
visited.add(poll); // visit it
q.addAll(graph.get(poll)); // and put its neighbourhood in the queue
}
}
visited.forEach(v -> graph.remove(v)); // removes the connected component from the graph
return visited;
}
示例8: parseTokensToValuesAndOperations
import java.util.Queue; //导入依赖的package包/类
@Override
public void parseTokensToValuesAndOperations(int j, String token, Queue<Float> values, Queue<String> operations) {
switch (token) {
case PLUS_CHAR:
case MINUS_CHAR:
// adding 0 as a first value when nothing before to start with a pair of values
if (j == 0) {
values.add(0f);
}
operations.add(token);
break;
default:
values.add(Float.parseFloat(token));
break;
}
}
示例9: width
import java.util.Queue; //导入依赖的package包/类
public static int width(TreeNode t) {
int maxWdith = 0, levelWidth = 0;
Queue<TreeNode> q = new LinkedList<>();
q.add(t);
q.add(null);
while (!q.isEmpty()) {
if (null == (t = q.poll())) {
if (levelWidth > maxWdith)
maxWdith = levelWidth;
levelWidth = 0;
if (null != q.peek()) q.add(null);
} else {
levelWidth++;
if (null != t.getLeft()) q.add(t.getLeft());
if (null != t.getRight()) q.add(t.getRight());
}
}
return maxWdith;
}
示例10: topologicalSort
import java.util.Queue; //导入依赖的package包/类
public Set<Vertex> topologicalSort(Vertex startNode,Set<Vertex> set){
Queue<Vertex> queue=new LinkedList<>();
queue.add(startNode);
while(!queue.isEmpty()){
Vertex u=queue.poll();
set.add(u);
for(Vertex v : u.adjacents)
{
if(!set.contains(v)){
v.inDegree--;
if(v.inDegree==0)
queue.add(v);
}
else
return new HashSet<Vertex>();
}
}
return set;
}
示例11: test
import java.util.Queue; //导入依赖的package包/类
void test(final Queue<Integer> q) throws Throwable {
System.out.println(q.getClass().getSimpleName());
pool = Executors.newCachedThreadPool();
print = false;
print = false;
System.out.println("Warmup...");
oneRun(1, items, q);
oneRun(3, items, q);
print = true;
for (int i = 1; i <= maxStages; i += (i+1) >>> 1) {
oneRun(i, items, q);
}
pool.shutdown();
check(pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
}
示例12: concurrentQueues
import java.util.Queue; //导入依赖的package包/类
Collection<Queue<Boolean>> concurrentQueues() {
List<Queue<Boolean>> queues = new ArrayList<Queue<Boolean>>();
queues.add(new ConcurrentLinkedDeque<Boolean>());
queues.add(new ConcurrentLinkedQueue<Boolean>());
queues.add(new ArrayBlockingQueue<Boolean>(count, false));
queues.add(new ArrayBlockingQueue<Boolean>(count, true));
queues.add(new LinkedBlockingQueue<Boolean>());
queues.add(new LinkedBlockingDeque<Boolean>());
queues.add(new LinkedTransferQueue<Boolean>());
// Following additional implementations are available from:
// http://gee.cs.oswego.edu/dl/concurrency-interest/index.html
// queues.add(new SynchronizedLinkedListQueue<Boolean>());
// Avoid "first fast, second slow" benchmark effect.
Collections.shuffle(queues);
return queues;
}
示例13: computeSpreadBlockLight
import java.util.Queue; //导入依赖的package包/类
private void computeSpreadBlockLight(int x, int y, int z, int currentLight, Queue<Vector3> queue,
Map<BlockVector3, Boolean> visited) {
int current = this.getBlockLightAt(x, y, z);
BlockVector3 index = Level.blockHash(x, y, z);
if (current < currentLight) {
this.setBlockLightAt(x, y, z, currentLight);
if (!visited.containsKey(index)) {
visited.put(index, true);
if (currentLight > 1) {
queue.add(new Vector3(x, y, z));
}
}
}
}
示例14: traverse
import java.util.Queue; //导入依赖的package包/类
void traverse(Node root) {
Queue<Node> layer = new LinkedList<>();
layer.offer(root);
while (!layer.isEmpty()) {
Queue<Node> nextLayer = new LinkedList<>();
Node node = layer.poll();
while (node != null) {
System.out.println(node.name);
if (node.left != null) {
nextLayer.offer(node.left);
}
if (node.right != null) {
nextLayer.offer(node.right);
}
node = layer.poll();
}
layer = nextLayer;
}
}
示例15: computeTargetShare
import java.util.Queue; //导入依赖的package包/类
protected void computeTargetShare(int nrStudents, Collection<CurriculumCourse> courses, CurriculumCourseGroupsProvider course2groups, CurModel model) {
for (CurriculumCourse c1: courses) {
float x1 = c1.getPercShare() * nrStudents;
Set<CurriculumCourse>[] group = new HashSet[] { new HashSet<CurriculumCourse>(), new HashSet<CurriculumCourse>()};
Queue<CurriculumCourse> queue = new LinkedList<CurriculumCourse>();
queue.add(c1);
Set<CurriculumCourseGroup> done = new HashSet<CurriculumCourseGroup>();
while (!queue.isEmpty()) {
CurriculumCourse c = queue.poll();
for (CurriculumCourseGroup g: course2groups.getGroups(c))
if (done.add(g))
for (CurriculumCourse x: courses)
if (!x.equals(c) && !x.equals(c1) && course2groups.getGroups(x).contains(g) && group[group[0].contains(c) ? 0 : g.getType()].add(x))
queue.add(x);
}
for (CurriculumCourse c2: courses) {
float x2 = c2.getPercShare() * nrStudents;
if (c1.getUniqueId() >= c2.getUniqueId()) continue;
float share = c1.getPercShare() * c2.getPercShare() * nrStudents;
boolean opt = group[0].contains(c2);
boolean req = !opt && group[1].contains(c2);
model.setTargetShare(c1.getUniqueId(), c2.getUniqueId(), opt ? 0.0 : req ? Math.min(x1, x2) : share, true);
}
}
}