如何一次将元素添加到另一个队列中?
How to add element into another queue at once?
我有一个 class 启动所有线程。
BlockingQueue<MyClass> otherQueue1 = new ArrayBlockingQueue<MyClass>(length);
BlockingQueue<MyClass> niceQueue = new ArrayBlockingQueue<MyClass>(length);
service = Executors.newFixedThreadPool(length);
for (int k = 0; k < length; k++) {
service.submit(new MyClass(otherQueue1, id));
}
在这个 for
循环中,我想将这个创建的 MyClass
放到 niceQueue
上。怎么做?
BlockingQueue<MyClass> otherQueue1 = new ArrayBlockingQueue<MyClass>(length);
BlockingQueue<MyClass> niceQueue = new ArrayBlockingQueue<MyClass>(length);
service=Executors.newFixedThreadPool(length);
for(int k=0; k < length; k++){
MyClass myClass = new MyClass(otherQueue1, id); #save reference in local variable
niceQueue.add(myClass); #add saved reference to queue
service.submit(myClass); #submit, same as in your example
}
}
我有一个 class 启动所有线程。
BlockingQueue<MyClass> otherQueue1 = new ArrayBlockingQueue<MyClass>(length);
BlockingQueue<MyClass> niceQueue = new ArrayBlockingQueue<MyClass>(length);
service = Executors.newFixedThreadPool(length);
for (int k = 0; k < length; k++) {
service.submit(new MyClass(otherQueue1, id));
}
在这个 for
循环中,我想将这个创建的 MyClass
放到 niceQueue
上。怎么做?
BlockingQueue<MyClass> otherQueue1 = new ArrayBlockingQueue<MyClass>(length);
BlockingQueue<MyClass> niceQueue = new ArrayBlockingQueue<MyClass>(length);
service=Executors.newFixedThreadPool(length);
for(int k=0; k < length; k++){
MyClass myClass = new MyClass(otherQueue1, id); #save reference in local variable
niceQueue.add(myClass); #add saved reference to queue
service.submit(myClass); #submit, same as in your example
}
}