简体   繁体   English

如何使用ArrayBlockingQueue初始化阻塞队列的大小 <String> ()显示在另一个线程中获取msg

[英]how can initialize size of blocking queue with ArrayBlockingQueue<String>() to show get msg in another thread

BlockingQueue<String> queue = new ArrayBlockingQueue<String>(10);

for(int i=0; i<queue.size(); i++){
    queue[i] = new ArrayBlockingQueue<String>();
}

System.out.println("queue size in main "+queue.size());

***// queue.size() returning 0 why???***

Thread t2 = new Thread(new AccountTransaction(queue));
t2.start();
Thread t1 = new Thread(new BankInfo(queue));

You are initializing your queue as an ArrayBlockingQueue , but referencing it later as an array of BlockingQueue s. 您正在将queue初始化为ArrayBlockingQueue ,但稍后将其引用为BlockingQueue的数组。

That won't compile. 那不会编译。

On top of that, the size method will return the number of items in the queue, not its capacity (which you set in the constructor). 最重要的是, size方法将返回队列中的项目数,而不是其容量(您在构造函数中设置的容量)。

When you reference queue.size() in your code it will return 0 , as there are no items in the queue. 当您在代码中引用queue.size()时,它将返回0 ,因为队列中没有项目。

In the condition of the for loop, when you call queue.size() it returns zero and the loop terminates without running the body. 在for循环的条件下,当您调用queue.size()时,它将返回零,并且循环将终止而不运行主体。 The output of the System.out.println() also calls queue.size() which is still zero. System.out.println()的输出还调用queue.size(),该参数仍然为零。 Try using http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ArrayBlockingQueue.html#remainingCapacity() 尝试使用http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ArrayBlockingQueue.html#remainingCapacity()

@mena also mentioned that you are using queue -- a collection -- as if it is an array which is a compile error. @mena还提到您正在使用队列(一个集合),就好像它是一个编译错误的数组一样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查ArrayBlockingQueue是否被锁定或队列元素当前是否由线程处理? - How to check if ArrayBlockingQueue is locked or queue element currently processed by thread? 如何从队列(ArrayBlockingQueue)中获取多个项目? - how can I get more than one item from the queue (ArrayBlockingQueue)? Java ArrayBlockingQueue 阻塞直到队列中有东西 - Java ArrayBlockingQueue blocking until the queue has something in it 如何在不使用阻塞队列的情况下将消息传递给另一个线程? - How can I pass messages to another thread without using a blocking queue? 阻塞队列不阻塞线程? - Blocking queue is not blocking the thread? 如何估计或计算ArrayBlockingQueue的大小 - How to estimate or calculate the size of the ArrayBlockingQueue 如何阻止阻塞队列大小增加线程池执行器服务? - How to stop blocking queue size from increasing in thread pool executor service? ArrayBlockingQueue-如何“中断”正在使用.take()方法的线程 - ArrayBlockingQueue - How to “interrupt” a thread that is wating on .take() method 为什么ArrayBlockingQueue被称为有界队列而LinkedBlockingQueue被称为无界阻塞队列? - Why the ArrayBlockingQueue is called a bounded queue while a LinkedBlockingQueue is called an unbounded blocking queue? 阻塞队列在 java 中不起作用,我可以放置比阻塞队列定义大小更多的元素 - Blocking queue not working in java, I can put more element than the defined size of blocking queue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM