A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王乐 中级黑马   /  2012-10-13 23:51  /  2022 人查看  /  13 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

下面这段代码,编译时提示
D:\jdk1.7.0_07\bin>javac Lock.java
Lock.java:9: 错误: 不兼容的类型
        private Lock lock=new ReentrantLock();
                          ^
  需要: Lock
  找到:    ReentrantLock

可是 ReentrantLock不是Lock接口的子类实现对象吗?创建了 ReentrantLock对象用Lock类型引用不是多态吗?
为什么会编译失败?

下面附上代码。请大家帮忙编一下
import java.util.concurrent.locks.*;
       

class ZiYuan
{
        private String name;
        private boolean flag;  
        private int num;
        private Lock lock=new ReentrantLock();


        Condition Condition_set=lock.newCondition();
        Condition Condition_get=lock.newCondition();
       
        void set(String name) throws Exception
        {
                lock.lock();
                try
                {
                while(flag)
                        Condition_set.await();
                this.name=name+" "+num++;
                System.out.println(Thread.currentThread().getName()+this.name);
                flag=true;
                Condition_get.signal();
                }
                finally
                {
                        lock.unlock();
                }
        }

        void get()throws Exception
        {
                lock.lock();
                try
                {
                        while(!flag)
                                Condition_get.await();
                System.out.println(Thread.currentThread().getName()+"xiaofei zhe"+this.name);
                flag=false;
                Condition_set.signal();
                }
                finally
                {
                        lock.unlock();
               
                }
        }
}



class ShengChan implements Runnable
{
        private ZiYuan z;
        ShengChan(ZiYuan z)
        {
                this.z=z;
        }
        public void run()
        {
                while(true)
                {
                        try
                        {
                                z.set("生产者");
                        }
                        catch(Exception e){}
                }
               
        }
}



class XiaoFei implements Runnable
{
        private ZiYuan z;
        XiaoFei(ZiYuan z)
        {
                this.z=z;
        }
        public void run()
        {
                while(true)
                {
                        try{
                                z.get();
                        }
                        catch(Exception e){}
                }
        }
}



class Lock
{
        public static void main(String[] args)
        {
                ZiYuan z=new ZiYuan();
                ShengChan s=new ShengChan(z);
                XiaoFei x=new XiaoFei(z);
                Thread t1=new Thread(s);
                Thread t2=new Thread(s);
                Thread t3=new Thread(x);
                Thread t4=new Thread(x);       
                t1.start();
                t2.start();
                t3.start();
                t4.start();
                       
        }
}

评分

参与人数 1技术分 +1 收起 理由
谭立文 + 1 神马都是浮云

查看全部评分

13 个回复

倒序浏览
请注意。你自定义了一个Lock类!
private Lock lock  这个则变成了你自定义类类型的属性,所以不能当做接口了

把下面Lock类名改了就好

评分

参与人数 1技术分 +1 收起 理由
谭立文 + 1 赞一个!

查看全部评分

回复 使用道具 举报
接口本来就不能实例对象化,所以无法作为一个引用变量
回复 使用道具 举报
本帖最后由 王震阳 于 2012-10-14 09:46 编辑

ReentrantLock lock = new ReentrantLock();//该处错误,以帮楼主修改,现在运行以没问题。
  1. import java.util.concurrent.locks.*;
  2.         

  3. class ZiYuan
  4. {
  5.         private String name;
  6.         private boolean flag;  
  7.         private int num;
  8.          ReentrantLock lock = new ReentrantLock();//该处错误,以帮楼主修改,现在运行以没问题。



  9.         Condition Condition_set=lock.newCondition();
  10.         Condition Condition_get=lock.newCondition();
  11.         
  12.         void set(String name) throws Exception
  13.         {
  14.                 lock.lock();
  15.                 try
  16.                 {
  17.                 while(flag)
  18.                         Condition_set.await();
  19.                 this.name=name+" "+num++;
  20.                 System.out.println(Thread.currentThread().getName()+this.name);
  21.                 flag=true;
  22.                 Condition_get.signal();
  23.                 }
  24.                 finally
  25.                 {
  26.                         lock.unlock();
  27.                 }
  28.         }

  29.         void get()throws Exception
  30.         {
  31.                 lock.lock();
  32.                 try
  33.                 {
  34.                         while(!flag)
  35.                                 Condition_get.await();
  36.                 System.out.println(Thread.currentThread().getName()+"xiaofei zhe"+this.name);
  37.                 flag=false;
  38.                 Condition_set.signal();
  39.                 }
  40.                 finally
  41.                 {
  42.                         lock.unlock();
  43.                
  44.                 }
  45.         }
  46. }



  47. class ShengChan implements Runnable
  48. {
  49.         private ZiYuan z;
  50.         ShengChan(ZiYuan z)
  51.         {
  52.                 this.z=z;
  53.         }
  54.         public void run()
  55.         {
  56.                 while(true)
  57.                 {
  58.                         try
  59.                         {
  60.                                 z.set("生产者");
  61.                         }
  62.                         catch(Exception e){}
  63.                 }
  64.                
  65.         }
  66. }



  67. class XiaoFei implements Runnable
  68. {
  69.         private ZiYuan z;
  70.         XiaoFei(ZiYuan z)
  71.         {
  72.                 this.z=z;
  73.         }
  74.         public void run()
  75.         {
  76.                 while(true)
  77.                 {
  78.                         try{
  79.                                 z.get();
  80.                         }
  81.                         catch(Exception e){}
  82.                 }
  83.         }
  84. }



  85. class Lock
  86. {
  87.         public static void main(String[] args)
  88.         {
  89.                 ZiYuan z=new ZiYuan();
  90.                 ShengChan s=new ShengChan(z);
  91.                 XiaoFei x=new XiaoFei(z);
  92.                 Thread t1=new Thread(s);
  93.                 Thread t2=new Thread(s);
  94.                 Thread t3=new Thread(x);
  95.                 Thread t4=new Thread(x);        
  96.                 t1.start();
  97.                 t2.start();
  98.                 t3.start();
  99.                 t4.start();
  100.                         
  101.         }
  102. }



复制代码

lock.jpg (71.5 KB, 下载次数: 26)

lock.jpg

评分

参与人数 1技术分 +1 收起 理由
唐志兵 + 1 赞一个!

查看全部评分

回复 使用道具 举报
ReentrantLock lock = new ReentrantLock();//该处错误,以帮楼主修改,现在运行以没问题。
  1. import java.util.concurrent.locks.*;
  2.         

  3. class ZiYuan
  4. {
  5.         private String name;
  6.         private boolean flag;  
  7.         private int num;
  8.          ReentrantLock lock = new ReentrantLock();//该处错误,以帮楼主修改,现在运行以没问题。



  9.         Condition Condition_set=lock.newCondition();
  10.         Condition Condition_get=lock.newCondition();
  11.         
  12.         void set(String name) throws Exception
  13.         {
  14.                 lock.lock();
  15.                 try
  16.                 {
  17.                 while(flag)
  18.                         Condition_set.await();
  19.                 this.name=name+" "+num++;
  20.                 System.out.println(Thread.currentThread().getName()+this.name);
  21.                 flag=true;
  22.                 Condition_get.signal();
  23.                 }
  24.                 finally
  25.                 {
  26.                         lock.unlock();
  27.                 }
  28.         }

  29.         void get()throws Exception
  30.         {
  31.                 lock.lock();
  32.                 try
  33.                 {
  34.                         while(!flag)
  35.                                 Condition_get.await();
  36.                 System.out.println(Thread.currentThread().getName()+"xiaofei zhe"+this.name);
  37.                 flag=false;
  38.                 Condition_set.signal();
  39.                 }
  40.                 finally
  41.                 {
  42.                         lock.unlock();
  43.                
  44.                 }
  45.         }
  46. }



  47. class ShengChan implements Runnable
  48. {
  49.         private ZiYuan z;
  50.         ShengChan(ZiYuan z)
  51.         {
  52.                 this.z=z;
  53.         }
  54.         public void run()
  55.         {
  56.                 while(true)
  57.                 {
  58.                         try
  59.                         {
  60.                                 z.set("生产者");
  61.                         }
  62.                         catch(Exception e){}
  63.                 }
  64.                
  65.         }
  66. }



  67. class XiaoFei implements Runnable
  68. {
  69.         private ZiYuan z;
  70.         XiaoFei(ZiYuan z)
  71.         {
  72.                 this.z=z;
  73.         }
  74.         public void run()
  75.         {
  76.                 while(true)
  77.                 {
  78.                         try{
  79.                                 z.get();
  80.                         }
  81.                         catch(Exception e){}
  82.                 }
  83.         }
  84. }



  85. class Lock
  86. {
  87.         public static void main(String[] args)
  88.         {
  89.                 ZiYuan z=new ZiYuan();
  90.                 ShengChan s=new ShengChan(z);
  91.                 XiaoFei x=new XiaoFei(z);
  92.                 Thread t1=new Thread(s);
  93.                 Thread t2=new Thread(s);
  94.                 Thread t3=new Thread(x);
  95.                 Thread t4=new Thread(x);        
  96.                 t1.start();
  97.                 t2.start();
  98.                 t3.start();
  99.                 t4.start();
  100.                         
  101.         }
  102. }
复制代码
回复 使用道具 举报
ReentrantLock lock = new ReentrantLock();//该处错误,以帮楼主修改,现在运行以没问题。
  1. import java.util.concurrent.locks.*;
  2.         

  3. class ZiYuan
  4. {
  5.         private String name;
  6.         private boolean flag;  
  7.         private int num;
  8.          ReentrantLock lock = new ReentrantLock();//该处错误,以帮楼主修改,现在运行以没问题。



  9.         Condition Condition_set=lock.newCondition();
  10.         Condition Condition_get=lock.newCondition();
  11.         
  12.         void set(String name) throws Exception
  13.         {
  14.                 lock.lock();
  15.                 try
  16.                 {
  17.                 while(flag)
  18.                         Condition_set.await();
  19.                 this.name=name+" "+num++;
  20.                 System.out.println(Thread.currentThread().getName()+this.name);
  21.                 flag=true;
  22.                 Condition_get.signal();
  23.                 }
  24.                 finally
  25.                 {
  26.                         lock.unlock();
  27.                 }
  28.         }

  29.         void get()throws Exception
  30.         {
  31.                 lock.lock();
  32.                 try
  33.                 {
  34.                         while(!flag)
  35.                                 Condition_get.await();
  36.                 System.out.println(Thread.currentThread().getName()+"xiaofei zhe"+this.name);
  37.                 flag=false;
  38.                 Condition_set.signal();
  39.                 }
  40.                 finally
  41.                 {
  42.                         lock.unlock();
  43.                
  44.                 }
  45.         }
  46. }



  47. class ShengChan implements Runnable
  48. {
  49.         private ZiYuan z;
  50.         ShengChan(ZiYuan z)
  51.         {
  52.                 this.z=z;
  53.         }
  54.         public void run()
  55.         {
  56.                 while(true)
  57.                 {
  58.                         try
  59.                         {
  60.                                 z.set("生产者");
  61.                         }
  62.                         catch(Exception e){}
  63.                 }
  64.                
  65.         }
  66. }



  67. class XiaoFei implements Runnable
  68. {
  69.         private ZiYuan z;
  70.         XiaoFei(ZiYuan z)
  71.         {
  72.                 this.z=z;
  73.         }
  74.         public void run()
  75.         {
  76.                 while(true)
  77.                 {
  78.                         try{
  79.                                 z.get();
  80.                         }
  81.                         catch(Exception e){}
  82.                 }
  83.         }
  84. }



  85. class Lock
  86. {
  87.         public static void main(String[] args)
  88.         {
  89.                 ZiYuan z=new ZiYuan();
  90.                 ShengChan s=new ShengChan(z);
  91.                 XiaoFei x=new XiaoFei(z);
  92.                 Thread t1=new Thread(s);
  93.                 Thread t2=new Thread(s);
  94.                 Thread t3=new Thread(x);
  95.                 Thread t4=new Thread(x);        
  96.                 t1.start();
  97.                 t2.start();
  98.                 t3.start();
  99.                 t4.start();
  100.                         
  101.         }
  102. }
复制代码
回复 使用道具 举报
陈琦 中级黑马 2012-10-14 10:49:54
7#
梁世喜 发表于 2012-10-14 00:21
请注意。你自定义了一个Lock类!
private Lock lock  这个则变成了你自定义类类型的属性,所以不能当做接 ...

不明白 ,为什么  private Lock lock=new ReentrantLock(); 这句不行?
回复 使用道具 举报
陈琦 中级黑马 2012-10-14 10:50:04
8#
cehkongfu 发表于 2012-10-14 09:31
接口本来就不能实例对象化,所以无法作为一个引用变量

不明白 ,为什么  private Lock lock=new ReentrantLock(); 这句不行?
回复 使用道具 举报
陈琦 中级黑马 2012-10-14 10:50:14
9#
王震阳 发表于 2012-10-14 09:44
ReentrantLock lock = new ReentrantLock();//该处错误,以帮楼主修改,现在运行以没问题。 ...

不明白 ,为什么  private Lock lock=new ReentrantLock(); 这句不行?
回复 使用道具 举报
陈琦 发表于 2012-10-14 10:49
不明白 ,为什么  private Lock lock=new ReentrantLock(); 这句不行?

代码下面有写了一个class Lock

l楼主意思是想用Java提供的接口Lock (多态) ,而在此处定义了一个class

在private Lock lock 是,此处的Lock为楼主定义的Lock(类)。

如果要使用接口Lock。请指定包名或者将下面自定义的Lock类该名字。
这里楼主虽然import了包import java.util.concurrent.locks.*。但是在JVM编译时,JVM就近使用的是自定义类Lock 而不是接口Lock。
回复 使用道具 举报
陈琦 中级黑马 2012-10-14 13:58:26
11#
梁世喜 发表于 2012-10-14 11:09
代码下面有写了一个class Lock

l楼主意思是想用Java提供的接口Lock (多态) ,而在此处定义了一个class ...

哦哦,没看到,sorry
回复 使用道具 举报
王乐 中级黑马 2012-10-14 14:12:23
12#
梁世喜 发表于 2012-10-14 00:21
请注意。你自定义了一个Lock类!
private Lock lock  这个则变成了你自定义类类型的属性,所以不能当做接 ...

我把下面的Lock类的类名改了
可是还是不能通过编译。提示:

D:\jdk1.7.0_07\bin>javac Lock2.java
D:\jdk1.7.0_07\bin\Lock.java:54: 错误: 类重复: ShengChan
class ShengChan implements Runnable
^
D:\jdk1.7.0_07\bin\Lock.java:77: 错误: 类重复: XiaoFei
class XiaoFei implements Runnable
^
D:\jdk1.7.0_07\bin\Lock.java:98: 错误: 类重复: R
class R
^
Lock2.java:9: 错误: 无法访问Lock
        private Lock lock = new ReentrantLock();
                ^
  错误的源文件: D:\jdk1.7.0_07\bin\Lock.java
    文件不包含类Lock
    请删除该文件或确保该文件位于正确的源路径子目录中。


请问你机器上通过了吗?
是不是我的机器设置或者类的存放有问题?
回复 使用道具 举报
王乐 中级黑马 2012-10-14 14:14:45
13#
王震阳 发表于 2012-10-14 09:44
ReentrantLock lock = new ReentrantLock();//该处错误,以帮楼主修改,现在运行以没问题。 ...

你好,谢谢你解答
我还想问下
视频里老师是用LOCK类型的变量接收 ReentrantLock();对象的。
请问我的代码为什么不能用Lock而是得用 ReentrantLock才能通过编译?
错在里哪里?烦请指教
回复 使用道具 举报
本帖最后由 梁世喜 于 2012-10-14 14:26 编辑
王乐 发表于 2012-10-14 14:14
你好,谢谢你解答
我还想问下
视频里老师是用LOCK类型的变量接收 ReentrantLock();对象的。
  1. package com.pratice;
  2. //下面这段代码,编译时提示
  3. //D:\jdk1.7.0_07\bin>javac Lock.java
  4. //Lock.java:9: 错误: 不兼容的类型
  5. //        private Lock lock=new ReentrantLock();
  6. //                          ^
  7. //  需要: Lock
  8. //  找到:    ReentrantLock
  9. //
  10. //可是 ReentrantLock不是Lock接口的子类实现对象吗?创建了 ReentrantLock对象用Lock类型引用不是多态吗?
  11. //为什么会编译失败?
  12. //
  13. //下面附上代码。请大家帮忙编一下
  14. import java.util.concurrent.locks.*;
  15. class ZiYuan
  16. {
  17.         private String name;
  18.         private boolean flag;  
  19.         private int num;
  20.         private Lock lock=new ReentrantLock();


  21.         Condition Condition_set=lock.newCondition();
  22.         Condition Condition_get=lock.newCondition();
  23.         
  24.         void set(String name) throws Exception
  25.         {
  26.                 lock.lock();
  27.                 try
  28.                 {
  29.                 while(flag)
  30.                         Condition_set.await();
  31.                 this.name=name+" "+num++;
  32.                 System.out.println(Thread.currentThread().getName()+this.name);
  33.                 flag=true;
  34.                 Condition_get.signal();
  35.                 }
  36.                 finally
  37.                 {
  38.                         lock.unlock();
  39.                 }
  40.         }

  41.         void get()throws Exception
  42.         {
  43.                 lock.lock();
  44.                 try
  45.                 {
  46.                         while(!flag)
  47.                                 Condition_get.await();
  48.                 System.out.println(Thread.currentThread().getName()+"xiaofei zhe"+this.name);
  49.                 flag=false;
  50.                 Condition_set.signal();
  51.                 }
  52.                 finally
  53.                 {
  54.                         lock.unlock();
  55.                
  56.                 }
  57.         }
  58. }



  59. class ShengChan implements Runnable
  60. {
  61.         private ZiYuan z;
  62.         ShengChan(ZiYuan z)
  63.         {
  64.                 this.z=z;
  65.         }
  66.         public void run()
  67.         {
  68.                 while(true)
  69.                 {
  70.                         try
  71.                         {
  72.                                 z.set("生产者");
  73.                         }
  74.                         catch(Exception e){}
  75.                 }
  76.                
  77.         }
  78. }



  79. class XiaoFei implements Runnable
  80. {
  81.         private ZiYuan z;
  82.         XiaoFei(ZiYuan z)
  83.         {
  84.                 this.z=z;
  85.         }
  86.         public void run()
  87.         {
  88.                 while(true)
  89.                 {
  90.                         try{
  91.                                 z.get();
  92.                         }
  93.                         catch(Exception e){}
  94.                 }
  95.         }
  96. }



  97. class LockTest//我将你原来的Lock改成了LockTest
  98. {
  99.         public static void main(String[] args)
  100.         {
  101.                 ZiYuan z=new ZiYuan();
  102.                 ShengChan s=new ShengChan(z);
  103.                 XiaoFei x=new XiaoFei(z);
  104.                 Thread t1=new Thread(s);
  105.                 Thread t2=new Thread(s);
  106.                 Thread t3=new Thread(x);
  107.                 Thread t4=new Thread(x);        
  108.                 t1.start();
  109.                 t2.start();
  110.                 t3.start();
  111.                 t4.start();
  112.                         
  113.         }
  114. }

复制代码
代码运行无错。类重复是因为你编写的文件中有同名的class 了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马