简体   繁体   English

使用显式锁来避免死锁会产生异常

[英]Using Explicit Lock to avoid deadlock gives an Exception

what i am trying to do here is there is a shared resource ie a SharedResource777.java, this class has two methods doIt() and setBFlag(), both of the thread acquire the Lock and execute the method using thread. 我在这里试图做的是有一个共享资源,即SharedResource777.java,此类有两个方法doIt()和setBFlag(),两个线程都获取Lock并使用线程执行该方法。

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]", 这里发生的是,在命令提示符下,第一行是“ THE THREAD TROY”,第二行是“ java.lang.IllegalMonitorStateException”,第三行是“ 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 . 我想做的是线程T1将执行doIt(),它反过来会获取锁定,而不是进入while循环,然后打印SOP wait,这将释放Lock。

Than thread t2 acquires the lock in method setBFlag() and signal() ie notify() to other thread that it has released the lock, 线程t2获得方法setBFlag()和signal()中的锁,即将其他线程已释放该锁的notify()通知给其他线程,

t1 will again acquire the lock and because of flag change, breaks the while loop , releases the lock in finally block. t1将再次获取锁定,并且由于标志更改,中断while循环,并在finally块中释放锁定。

But in my scenario i get an exception , 但是在我的情况下,我遇到了一个例外,

Can you please tell me where i am missing, 你能告诉我我想念的地方吗,

Where i am going wrong 我要去哪里了

I think your condition.wait(); 我认为你的condition.wait(); should really be condition.await(); 应该真的是condition.await();

They do very different things. 他们做的事情截然不同。

When calling wait()/notify() methods you should hold intrinsic object lock. 调用wait()/notify()方法时,应保持内部对象锁定。 You are calling condition.wait() and not condition.await() . 您正在调用condition.wait()而不是condition.await() This should be the reason of your IllegalMonitorState exception. 这应该是您的IllegalMonitorState异常的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM