[英]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.