简体   繁体   中英

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.

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).

When you reference queue.size() in your code it will return 0 , as there are no items in the queue.

In the condition of the for loop, when you call queue.size() it returns zero and the loop terminates without running the body. The output of the System.out.println() also calls queue.size() which is still zero. Try using 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM