黑马程序员技术交流社区

标题: 读写锁的异常 [打印本页]

作者: 丁朋伟    时间: 2011-9-24 06:32
标题: 读写锁的异常
下面的读写锁代码出现了IllegalMonitorStateException异常,查了好久没找出原因
        public static void main(String[] args) {
                final ReadWrite readWrite = new ReadWrite();
                for(int i=0;i<3;i++){
                        new Thread(){
                                public void run() {
                                        while(true){
                                                readWrite.read();
                                        }
                                }}.start();
                        new Thread(){
                                public void run() {
                                        while(true){
                                                readWrite.write(new Random().nextInt(1000));
                                        }
                                }}.start();
                }
        }
}
class ReadWrite{
        Object data = null;
        ReadWriteLock rwl = new ReentrantReadWriteLock();
        public void read(){
                rwl.readLock().lock();
                try{
                        System.out.println(Thread.currentThread().getName()+" ready to read data");
                        Thread.sleep((long)(Math.random()*1000));
                        System.out.println(Thread.currentThread().getName()+" has read " + data);
                }catch(InterruptedException e){
                        e.printStackTrace();
                }finally{
                        rwl.readLock().unlock();
                }
        }
        public void write(Object data){
                rwl.writeLock().unlock();
                try{
                        System.out.println(Thread.currentThread().getName()+" ready to write data");
                        this.data = data;
                        Thread.sleep((long)(Math.random()*1000));
                        System.out.println(Thread.currentThread().getName()+" has written " + data);
                }catch(InterruptedException e){
                        e.printStackTrace();
                }finally{
                        rwl.writeLock().unlock();
                }
               
        }
作者: 宋红旺    时间: 2011-9-24 08:20
嘿嘿,遇见这种问题不能慌,其实并不难,对于这种问题我的解决方案是
一:先看异常提示,大概划定异常出在那一部分
二:断点调试
三:基本问题搞定。
如果是比较复杂的程序,或很长的程序建议使用junit或别的插件调试会更快一点。嘿嘿,说的有点啰嗦,分享一下心得。
public static void main(String[] args) {
           final ReadWrite readWrite = new ReadWrite();
           for(int i=0;i<3;i++){
                   new Thread(){
                           public void run() {
                                   while(true){
                                           readWrite.read();
                                   }
                           }}.start();
                   new Thread(){
                           public void run() {
                                   while(true){
                                           readWrite.write(new Random().nextInt(1000));
                                   }
                           }}.start();
           }
   }
}
class ReadWrite{
   Object data = null;
   ReadWriteLock rwl = new ReentrantReadWriteLock();
   public void read(){
           rwl.readLock().lock();
           try{
                   System.out.println(Thread.currentThread().getName()+" ready to read data");
                   Thread.sleep((long)(Math.random()*1000));
                   System.out.println(Thread.currentThread().getName()+" has read " + data);
           }catch(InterruptedException e){
                   e.printStackTrace();
           }finally{
                   rwl.readLock().unlock();
           }
   }
   public void write(Object data){
//           rwl.writeLock().unlock(); //你这里写错了,你写的是解锁,而正确的是应该上锁。        
   rwl.writeLock().lock();
           try{
                   System.out.println(Thread.currentThread().getName()+" ready to write data");
                   this.data = data;
                   Thread.sleep((long)(Math.random()*1000));
                   System.out.println(Thread.currentThread().getName()+" has written " + data);
           }catch(InterruptedException e){
                   e.printStackTrace();
           }finally{
                   rwl.writeLock().unlock();
           }
           
   }
[ 本帖最后由 宋红旺 于 2011-09-24  08:22 编辑 ]
作者: 匿名    时间: 2011-9-24 08:23
哎呀,人才啊
鄙视自己竟然排错了那么长时间,受教




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2