刚发的比较凌乱 不好看 再发一个 生产者消费者 求助

csjackylongtao 2014-02-13 03:55:03
消费者:不断取数据
public class Consumer implements Runnable{
private Info info;
public Consumer(Info info){
this.info = info;}
public void run() {
for(int i=0;i<50;i++){
this.info.get();
}}}

生产者:不断变数据
public class Producer implements Runnable {
private Info info = null;
public Producer(Info info){
this.info = info;}
public void run() {
boolean flag = false;
for(int i=0;i<50;i++){
if(!flag){
this.info.set("xielongtao", "basketball");
flag = true;
}else{
this.info.set("谢龙涛","篮球");
flag = false;}}}}

信息类:生产者改他的数据 ,消费者取他的数据,flag是个标志位初始为false
public class Info {
private String name = "谢龙涛";
private String hobby = "篮球";
private boolean flag = false;
public String getName() {
return name;}
public void setName(String name) {
this.name = name;}
public String getHobby() {
return hobby;}
public void setHobby(String hobby) {
this.hobby = hobby;}
public synchronized void set(String name,String hobby){
System.out.println(Thread.currentThread().getName()+"set");
if (!flag) {
try {
this.wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
this.setName(name);
this.setHobby(hobby);
flag = false;
super.notify(); }}
public synchronized void get(){
System.out.println(Thread.currentThread().getName()+"get");
if (flag) {
try {
this.wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println("name:" + this.getName() + " hobby:"
+ this.getHobby());
flag = true;
super.notify();}}}

main方法
public class SycronizeTest {
public static void main(String[] args) {
Info info = new Info();
Consumer c = new Consumer(info);
Producer p = new Producer(info);
new Thread(c,"consumer").start();
new Thread(p,"producer").start();}}

我想控制线程输出一行中文,然后就输出一行英文
如下 谢龙涛 篮球
xielongtao basketball
可是运行main方法没有输出这是为什么? 死锁了吗 两个线程都在跑 有唤醒啊消费者为什么没有取到?
...全文
208 5 打赏 收藏 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
nmyangym 2014-02-13
  • 打赏
  • 举报
回复
个人浅见: 问题出在set 和get 方法里,以get方法为例,if语句有问题,假设消费线程得到锁,获得运行机会,同时flag 为false 表示可以取,但你的代码什么也没干,方法就结束了。所以这个地方有问题。所以要把可以消费的执行代码放在if语句块后边,不能放在if语句块里面,放在里面的话,只有flag为true时,进入等待,生产线程结束生产,唤醒等待线程时,才能运行。 所以代码需要稍微改一下:

   public  synchronized void  set(String name,String hobby){
        System.out.println(Thread.currentThread().getName()+"set");
        if (!flag) {// 表示是满的 不能加
            try {
                this.wait();
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            this.setName(name);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }//-------------------------------if 在此结束
            this.setHobby(hobby);
            flag = false;//让他取
            super.notify();            
        //}// -------------------------------这不要
    }
    public synchronized void get(){
        System.out.println(Thread.currentThread().getName()+"get");
        if (flag) {//表示是空的 不能取
            try {
                this.wait();
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }//----------------------------------------if 在此结束
            System.out.println("name:" + this.getName() + " hobby:"
                    + this.getHobby());
            flag = true;// 让他放
            super.notify();
        //}//------------------------------------- 这不要
    }
另外,最好用while 替代if,更安全。
kanorl 2014-02-13
  • 打赏
  • 举报
回复
flag = false consumer线程启动,循环调用info.get(),进不去if{},没机会nofity() producer线程启动,调用info.set(),进入if{},马上就wait()了,没机会修改flag
csjackylongtao 2014-02-13
  • 打赏
  • 举报
回复
我初衷想控制线程想输出一行中文,输出一行英文,如下 xielongtao basketball 谢龙涛 篮球 可是运行main方法没有输出这是为什么? 死锁了吗 两个线程都在跑 有唤醒啊消费者为什么没有取到?
csjackylongtao 2014-02-13
  • 打赏
  • 举报
回复
生产者

public class Producer implements Runnable {
	private Info info = null;
	public Producer(Info info){
		this.info = info;
	}
	@Override
	public void run() {
		 boolean flag = false;
		 for(int i=0;i<50;i++){
			 if(!flag){
				this.info.set("xielongtao", "basketball");
				flag = true;
			 }else{
				this.info.set("谢龙涛","篮球");
				flag = false;
			 }
		 }
		
	}
	
}
消费者

public class Consumer implements Runnable{
	private Info info;
	public Consumer(Info info){
		this.info = info;
	}
	@Override
	public void run() {
		for(int i=0;i<50;i++){
			this.info.get();
		}
	}

}
信息类

public class Info {
	private String name = "谢龙涛";
	private String hobby = "篮球";
	private boolean flag = false;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getHobby() {
		return hobby;
	}
	public void setHobby(String hobby) {
		this.hobby = hobby;
	} 
	public  synchronized void  set(String name,String hobby){
		System.out.println(Thread.currentThread().getName()+"set");
		if (!flag) {// 表示是满的 不能加
			try {
				this.wait();
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			this.setName(name);
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			this.setHobby(hobby);
			flag = false;//让他取
			super.notify();			
		}
	}
	public synchronized void get(){
		System.out.println(Thread.currentThread().getName()+"get");
		if (flag) {//表示是空的 不能取
			try {
				this.wait();
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("name:" + this.getName() + " hobby:"
					+ this.getHobby());
			flag = true;// 让他放
			super.notify();
		}
	}
	
}
main方法
public class SycronizeTest {
	public static void main(String[] args) {
		Info info = new Info();
		Consumer c = new Consumer(info);
		Producer p = new Producer(info);
		new Thread(c,"consumer").start();
		new Thread(p,"producer").start();
	}
}
求助啊 大神们~~~~~~~~~

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧