Results 1 to 3 of 3
- 03-28-2010, 11:45 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 3
- Rep Power
- 0
Need help with wait() and notify()
public class ThreadPool {
static ExecutorService pool;
static LinkedList<Runnable> list = new LinkedList<Runnable>();
static ThreadWorker threadWorker = new ThreadWorker();
static Thread worker = new Thread(threadWorker);
ThreadPool(){
pool = Executors.newSingleThreadExecutor();
pool.execute(worker);
}
ThreadPool(int size){
pool = Executors.newFixedThreadPool(size);
pool.execute(worker);
}
synchronized void add(Runnable task){
list.add(task);
notifyAll();
}
void stopPool(){
pool.shutdown();
}
}
class ThreadWorker implements Runnable{
public synchronized void run(){
while(true){
if(ThreadPool.list.size() == 0){
try {
System.out.println("Worker is waiting!!");
this.wait();
} catch (InterruptedException e) {
System.out.println(e);
}
}
else{
ThreadPool.list.poll().run();
}
}
}
}
It seems that the notifyAll() doesn't activate de wait() in the ThreadWorker class. I was wondering what i am doing wrong. thank you
-
Please don't bump your post with a link in two other threads or multipost a question.
Last edited by Fubarable; 03-28-2010 at 02:33 PM.
- 03-30-2010, 11:58 AM #3
Similar Threads
-
Wait() notify() implementation
By georges in forum Advanced JavaReplies: 4Last Post: 02-05-2010, 01:33 AM -
wait() and notify() trouble with UI
By Atriamax in forum Threads and SynchronizationReplies: 2Last Post: 12-09-2009, 02:51 AM -
wait() and notify() problems
By greyradio in forum Threads and SynchronizationReplies: 1Last Post: 08-03-2009, 03:36 AM -
What is the execution path of wait() and notify() ?
By AegisCruiser in forum Threads and SynchronizationReplies: 1Last Post: 04-23-2008, 06:16 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks