Rahul Shivsharan
Rahul Shivsharan

Reputation: 2561

Using Explicit Lock to avoid deadlock gives an Exception

what i am trying to do here is there is a shared resource i.e. a SharedResource777.java, this class has two methods doIt() and setBFlag(), both of the thread acquire the Lock and execute the method using thread.

the code is as below,

    import java.util.concurrent.locks.*;

    class SharedResource777{
        private boolean bFLag = false;
        private Lock lockObj = new ReentrantLock();
        private Condition condition = lockObj.newCondition();

        public void doIt() {        
            try{
                lockObj.lock();         
                while(!bFLag){
                    System.out.println(" THE THREAD "+Thread.currentThread().getName());            
                    condition.wait();
                }           
            }catch(Exception e){
                System.out.println(e);
            }finally{
                lockObj.unlock();
            }           
        }

        public void setBFlag(boolean bFLag){        
            try{
                lockObj.lock(); 
                this.bFLag = bFLag;     
                System.out.println(" THE THREAD "+Thread.currentThread().getName()+" ["+this.bFLag+"]");
                condition.signal();
            }catch(Exception e){
                System.out.println(e);
            }finally{
                lockObj.unlock();
            }
        }

    }

    class MyThread620 extends Thread{

        private SharedResource777 resource;

        MyThread620(String threadName,SharedResource777 resource){
            super(threadName);
            this.resource = resource;
        }

        @Override
        public  void run(){
            resource.doIt();            
        }
    }

    class MyThread621 extends Thread{

        private SharedResource777 resource;

        MyThread621(String threadName,SharedResource777 resource){
            super(threadName);
            this.resource = resource;
        }

        @Override
        public  void run(){
            resource.setBFlag(true);                    
        }
    }



    public class Ex11{
        public static void main(String [] args){
            SharedResource777 obj777 = new SharedResource777();
            MyThread620 t620 = new MyThread620("TROY",obj777);
            MyThread621 t621 = new MyThread621("HECTOR",obj777);
            t620.start();
            t621.start();
        }
    }

What happens here is, in command prompt the first line is "THE THREAD TROY", second line is "java.lang.IllegalMonitorStateException", third line is "THE THREAD HECTOR [true]",

and the program exits.

What i was trying to do is Thread T1 will execute doIt(), which in turn acquires the lock than enter the while loop, print the SOP wait , which will release the Lock .

Than thread t2 acquires the lock in method setBFlag() and signal() i.e notify() to other thread that it has released the lock,

t1 will again acquire the lock and because of flag change, breaks the while loop , releases the lock in finally block.

But in my scenario i get an exception ,

Can you please tell me where i am missing,

Where i am going wrong

Upvotes: 0

Views: 249

Answers (2)

Joachim Isaksson
Joachim Isaksson

Reputation: 180997

I think your condition.wait(); should really be condition.await();

They do very different things.

Upvotes: 6

Alex Nikolaenkov
Alex Nikolaenkov

Reputation: 2545

When calling wait()/notify() methods you should hold intrinsic object lock. You are calling condition.wait() and not condition.await(). This should be the reason of your IllegalMonitorState exception.

Upvotes: 0

Related Questions