java - How to take object from BlockingQueue? -
i have class x
, classmachine
, class z
, of them threads. when machine
threads initialized, put on blockingqueue firstqueue
. inside machines' run method there while loop checks if boolean variable should true or false. when true, machine should put on blockingqueue secondqueue
secondqueue.put(this);
and z
class can take there. if machine thread returns false, class x
can take machine firstqueue
, work on it. now, question is: when boolean true, possible make machine
take firstqueue
? ps.i know question might unclearly asked, don't know how form properly. if knows make better, please correct it.
edit. code samples. here part class starts threads, queues initialized of course.
public class machine implements runnable{ private final blockingqueue firstqueue; private final blockingqueue secondqueue; boolean broken; public machine(...){...} public void run() { while(true){ //use rand (min=0, max=1) , define if(random==1)broken=true else broken=false if(broken==true){ machine multi = fistqueue.take(this);//here problem!!!!! secondqueue.put(this); } } }...}
and part form class starts threads
//machines should have ability take queue when broken==true, , put on secondqueue blockingqueue<machine> firstqueue = new arrayblockingqueue<machine>(10); service=executors.newfixedthreadpool(10); for(int k=0; k < 10; k++){ machine m= new machine(secondqueue, k); firstqueue.add(m); service.submit(m); } //x can take machines firstqueue, , work on them if broken==false thread xthread = new thread(new x(firstqueue)); xthread.start(); //takes machines form secondqueue thread zthread = new thread(new z(secondqueue)); zthread.start();
edit2
public class x implements runnable(){ //fields, constructor public void run() { while(true){ machine=machines.take(); if(machine.broken==true){ //leave machine (it should on , , take other fine_ } while(machine.broken==false){ machine.pass(record); // (record record=new record(task, result field); //do thing } if(result==initialresultfromx){ //means got broken while working , needs take new machine } }... }
first of, answer aims improve design of solution, in turn might answer actual question. however, if op happy current design, believe question can answered removing following line:
machine multi = fistqueue.take(this);
so,
is possible make machine take firstqueue?
there no method directly object inside queue without removing (as stated in comments, machine should not removed first queue). because can access instance of machine using this
, secondqueue.put(this)
suffice in adding machine
second queue.
i might interpreting design wrong, seems me each machine
has state. state depends whether or not machine
can or cannot execute whatever must execute. if true, believe isn't wise keep handling of state changes in machine itself(adding/removing different executing queues).
you need abstraction of sort. lets called statemachine
. statemachine
creates, manages , handles state changes of each machine
, implementing listening interface. allow each machine report events or problems statemachine
. statemachine
can determine how handle events.
i'll try explain example. here interface statemachine
implement:
public interface brokenlistener { public void onbrokenevent(object machine); }
this allows communications between statemachine
, each machine
. however, requires instance of statemachine
passed each machine instead of queues.
for(int k=0; k < 10; k++){ machine m = new machine(this); //where statemachine firstqueue.add(m); }
once machines
state changes broken == false
broken == true
, onbrokenevent()
can called.
public class machine { /* listener */ private brokenlistener listener; private boolean broken = false; public machine(brokenlistener listener) { this.listener = listener; } /* when state of machine changes */ public void setbroken(boolean broken) { this.broken = broken; if (this.broken) { //here pass current machine statemachine. how event handled should not machine. this.listener.onbrokenevent(this); } } }
here statemachine
:
public class statemachine implements brokenlistener { @override public void onbrokenevent(object machine) { if (machine instanceof machine) { second.add((machine) machine); } } }
as can see, when state machine implements onbrokenevent
method. when method called machine
, can added second queue processing.
i assume x
, y
classes processing, still need pass queues them.
thread xthread = new thread(new x(firstqueue)); xthread.start(); thread zthread = new thread(new z(secondqueue)); zthread.start();
what makes nice is, keeps logic used handling state changes out of machine
.
feel free ask questions.
Comments
Post a Comment