java - how to use BlockingQueues -
i having problem blockingqueues. size of queue comes 1 first loop , 0 after there there. variables 'as' null.
public class board extends jpanel { private myclass mc; private thread combatthread; final blockingqueue<myclass> queue = new linkedblockingqueue<myclass>(); public board() { mc = new myclass(); mc.add(1); mc.add(2); combatthread = new thread(new cthread(queue)); test(); } public void test() { try { queue.add(mc); } catch(exception e1) {} combatthread.start(); } public class cthread implements runnable { private myclass as; public cthread(blockingqueue queue) { } public void run() { while( true ){ try { = queue.poll(); //as null } catch (exception e) {} } } } }
in constructor
public board()
you create 1 object - mc, , give 2 values (1,2)
in method
public void test()
you add 1 object queue
queue.add(mc);
so when thread starts, queue contains 1 (myclass)mc instance reference, , mc object should contain 1 , 2 values (i not sure because not give myclass source). first iteration in
while( true ){ try { = queue.poll(); //as null } catch (exception e) {} }
will assign "as" variable reference "mc". how know object kind of queue, why think asarray method should work? implement or inherit somewhere? suggest not. if ask blockingqueue mess.
myclass - here wrong. class name should start capital letter. read java name conventions.
this constructor confuse too, nothing, why use it?
public cthread(blockingqueue queue) { }
i think should read http://javarevisited.blogspot.com/2012/02/producer-consumer-design-pattern-with.html, see how blockingqueue works, see not understand basics, may should start beginners books variable references, inheritance, collections , after return task.
Comments
Post a Comment