java - Queue of Thread objects -


i trying have threads on queue can manage them there. possible? have code don't work correctly.

the main idea generate x threads , put every thread inside queue in class. in class have queue use wait() , notify() methods have fifo execution order.

thanks in advance.

some of code:

public synchronized void semwait(thread petitionerthread){      count--;      if(count < 0){          try {             petitionerthread.wait();         } catch (interruptedexception e) {             e.printstacktrace();         }         fifoqueue.add(petitionerthread);         } }  public synchronized void semsignal(thread noticethread){      count++;      if(count <= 0)         if(!fifoqueue.isempty())             fifoqueue.polllast().notify(); } 

edit: problem when thread enters queue , put wait, happen semsignal method never executed of other threads (its called after semwait()).

you might want check blockingqueue(concrete class linkedblockingqueue) in java. queue allows put object it, matter thread. queue.put() wait if queue full. , queue.get() wait if queue empty. wait() , notify() implicitly taken care of. set of threads can take queue , execute them in order. talking of producer-consumer problem.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -