我有一个启动所有线程的类。
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
。怎么做?
答案 0 :(得分:0)
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
}
}