黑马程序员技术交流社区
标题:
多线程中的问题
[打印本页]
作者:
途中ms前进
时间:
2015-5-29 23:10
标题:
多线程中的问题
import java.util.concurrent.locks.*;
public class Test {
public static void main(String[] args){
res a=new res();
Pru p=new Pru(a);
Con c=new Con(a);
Thread t1=new Thread(p);
Thread t2=new Thread(c);
Thread t3=new Thread(p);
Thread t4=new Thread(c);
}
}
class res{
private String name;
private int count=1;
private boolean flag=false;
private Lock lock=new ReentrantLock();
private Condition condition_p=lock.newCondition();
private Condition condition_c=lock.newCondition();
res(String name){
this.name=name+"..."+count++;
}
public void set(String name)throws InterruptedException{
lock.lock();
try{while(flag)
condition_p.await();
System.out.println(Thread.currentThread().getName()+"生产者:::"+this.name);
condition_c.signalAll();
}
finally{
lock.unlock();
}
}
public void out()throws InterruptedException{
lock.lock();
try{while(!flag)
condition_c.signal();
System.out.println(Thread.currentThread().getName()+"消费者:::"+this.name);
condition_p.signalAll();
}
finally{
lock.unlock();
}
}
}
class Pru implements Runnable{
private res r;
Pru(res r){
this.r=r;
}
public void run(){
try{while(true)
r.set("商品");
}
catch(Exception e){
}
}
}
class Con implements Runnable{
private res r;
Con(res r){
this.r=r;
}
public void run(){
try{while(true)
r.out();
}
catch(Exception e){
}
}
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The constructor reas() is undefined
at itheima.Test.main(Test.java:7)
}//为啥在Eclipse上会出现这个问题 求大神解释
作者:
wangkai
时间:
2015-5-30 10:52
飘过学习学习
作者:
kmlitheima
时间:
2015-5-30 11:00
新来的,在摸索中学习!
作者:
曲终烟尽
时间:
2015-5-30 11:06
代码第7行 reas()没有定义
作者:
志行
时间:
2015-5-30 13:37
额 哪里蹦出来的reas () 我只看到了res () a ~~~~
作者:
tougboy
时间:
2015-5-30 20:51
我的编辑器也经常这样 试试百度瞧瞧
作者:
tougboy
时间:
2015-5-30 20:52
我的编辑器也经常这样 试试百度瞧瞧
作者:
夏尔
时间:
2015-5-30 21:18
你这线程都没启动。
作者:
feng0606
时间:
2015-5-30 21:39
刚看到这里,不是要start才能运行么?
作者:
开弓没有回头箭
时间:
2015-5-30 21:40
只定义了有参数的构造函数,没定义空参数的构造函数
作者:
零点Crazy
时间:
2015-5-30 23:04
Thread t1=new Thread(p).statrt()。
作者:
武汉小菜鸟
时间:
2015-5-30 23:41
兄弟,首先你的代码有两个问题,
一,没有start(),没有启动线程
二,System.out.println(Thread.currentThread().getName()+"生产者:::"+this.name);
condition_c.signalAll(); 这个代码应该放在try外面。
作者:
wangkai
时间:
2015-5-31 00:46
线程没启动 而且是不是包名和工程名字不一样 你再看看
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2