I have two Java classes
public class Firstclass
{
public static void main(String args[]) throws InterruptedException
{
System.out.println("Main start....");
Secondclass s=new Secondclass();
Thread t1 = new Thread(s);
t1.setName("First Thread");
Thread t2=new Thread(s);
t2.setName("Second Thread");
t1.start();
t2.start();
System.out.println("Main close...");
}
}
and
public class Secondclass implements Runnable
{
public static Object obj;
int counter=10;
public void inc()
{
counter++;
}
public void dec()
{
counter--;
}
@Override
public void run()
{
try
{
loop();
}
catch(Exception e)
{
System.out.println("exception is"+e);
}
}
public void loop() throws InterruptedException
{
for(int i=0;i<10;i++)
{
if(Thread.currentThread().getName().equals("First Thread"))
{
Thread.currentThread().wait();
inc();
}
else
{
dec();
}
System.out.println("Counter=="+counter);
}
}
}
My first question is: I want my first thread to wait until second thread completes and I am able to acheive that but in the output I am getting the exception java.lang.IllegalMonitorStateException
.I don't know why.
My second question is: Can you please guide me any tutorial site where I can learn wait(), notify and notifyall() methods from basics.