• Post Reply Bookmark Topic Watch Topic
  • New Topic

Not able to run the threads, any help pls...

 
rahul karwal
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do i have to do to make sure that both the threads execute? Right now i am getting IllegalMonitorStateException? Can anyone pls tell me what i am doing wrong ?
thanks
rahul

import java.util.Iterator;
import java.util.ArrayList;

public class MyTest{

public static int CONSUMER_SLEEP=1;
public static int PRODUCER_SLEEP=5;

public static void main(String[]args)
{
ArrayList list = new ArrayList();
Consumer cons = new Consumer(list);
Producer prod = new Producer(list);
Thread consThread = new Thread(cons,"consumer");
Thread prodThread = new Thread(prod,"producer");
consThread.start();
prodThread.start();
}

static class Consumer implements Runnable{
ArrayList consumerList;

public Consumer(ArrayList consumerList)
{
this.consumerList = consumerList;
System.out.println("in consumer");

}

public void run()
{
for(int i=0;i<1000000;i++)
{
synchronized(consumerList)
{
consumerList.add(new Integer(i));
}
System.out.println("consumerList = "+ consumerList);
try
{
System.out.println("giong to wait from consumer");
wait(CONSUMER_SLEEP);
System.out.println("got notified from consumer");
notify();

}
catch(InterruptedException e)
{

}
}
}
}


static class Producer implements Runnable{
ArrayList producerList;
ArrayList ownList= new ArrayList();
public Producer(ArrayList producerList)
{
this.producerList = producerList;
System.out.println("in producer");
}

public void run()
{
if(producerList.size()<1000)
{
synchronized(producerList)
{
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
for(Iterator iter=producerList.iterator();iter.hasNext()
{
ownList.add(iter.next());
iter.remove();
System.out.println("producerList = "+producerList);
try
{
System.out.println("giong to notify from producer");
notify();
wait(PRODUCER_SLEEP);
System.out.println("got notified from producer");


}
catch(InterruptedException e)
{

}
}
}
}
}
}


}
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IllegalMonitorStateException always means you've called either wait(), notify(), or notifyAll() on an object without being inside a block or method synchronized on that object. You're calling wait() and notify() on your Consumer and Producer Runnables, but you're never synchronizing on these objects -- hence the exceptions.

But it doesn't seem like you understand how to use wait and notify -- do you realize that multiple threads that are communicating via wait() and notify() must all be calling these methods on the same object?
 
rahul karwal
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest for replying,
I bascially want to synchronize my Common ArrayList(ie list) which both of my classes are using (ie consumer and producer). How do i synchronize so that this arraylist can be used only by one thread without throwing IllegalStateExceptoin.
thanks
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the classic producer/consumer example from Sun's Java Tutorial; you might want to back up and read the whole Threads chapter here.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!