【java小白】程序在多线程中使用if和if else 遇到如下问题,请问这个该怎么理解?[ Java SE]

Lincain 2017-08-06 08:03:19


/* 该程序需要解决如下问题:
* Producter类负责向Resoure类存储姓名和性别两个字符串;当i=偶数时,储存“李雷+男”
* 当为奇数时,储存“韩梅梅+女”。
* Consumer类负责从Resoure类中取出以上数据。
* 需要达到的效果是Producter类每储存一次数据,Consumer类就从中取出一次数据。
* 控制台上间隔显示“李雷--男,韩梅梅--女”。
*
*/
public class ThreadTest2 {
public static void main(String[] args) {
Resoure resoure=new Resoure();
new Thread(new Producter(resoure)).start();
new Thread(new Consumer(resoure)).start();
}
}
class Producter implements Runnable {
private Resoure resoure=null;
public Producter(Resoure resoure) {
this.resoure = resoure;
}
public void run() {
for(int i=0;i<50;i++) {
if(i%2==0){
resoure.setinfo("李雷", "男");
}else {
resoure.setinfo("韩梅梅","女");
}
}
}
}
class Consumer implements Runnable{
private Resoure resoure=null;
public Consumer(Resoure resoure) {
this.resoure = resoure;
}
public void run() {
for(int i=0;i<50;i++) {
resoure.getinfo();
}
}
}
class Resoure {
private String name;
private String gender;
private Boolean isExmpty=true;
synchronized public void setinfo(String name,String gender){
try {
if(!isExmpty) {
this.wait();
}
this.name=name;
Thread.sleep(10);
this.gender=gender;
isExmpty=false;
this.notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

synchronized public void getinfo(){
try {
if(isExmpty) {
this.wait();
}
System.out.println(this.name+"--"+this.gender);
Thread.sleep(10);
isExmpty=true;
this.notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

程序如下:
现在遇到如下问题:
在Resoure类中方法setinfo()和getinfo()方法中的if代码,如果采用if else代码,用如下代码替换就不能达到预定的效果,但是个人感觉两者是一样的效果,我的理解是这样的:如果if语句为true,则执行this.wait();下面的语句就不执行,否则就执行else代码块里面的内容,和上面不加else是一样的意思,但是结果却大相径庭,所以想请教一下这个该怎么理解?非常感谢
try {
if(!isExmpty) {
this.wait();
}else{
this.name=name;
Thread.sleep(10);
this.gender=gender;
isExmpty=false;
this.notify();
}



...全文
422 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
三仙半 2017-08-09
  • 打赏
  • 举报
回复
Resoure类

import java.util.ArrayList;
import java.util.List;

/**
 * @author ZhengYesheng
 */
public class Resoure
{
	private List<String> names = new ArrayList<>();

	public void put(String name)
	{
		if (names.size() == 1)
		{
			synchronized (this)
			{
				try
				{
					this.wait();
				}
				catch (InterruptedException e)
				{
					e.printStackTrace();
				}
			}
		}
		names.add(name);
		this.notifyAll();
	}

	public String get()
	{
		if (names.size() == 0)
		{
			synchronized (this)
			{
				try
				{
					this.wait();
				}
				catch (InterruptedException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		String name = names.remove(0);
		this.notifyAll();
		return name;
	}
}
Producter类

/**
 * @author ZhengYesheng
 */
public class Producter extends Thread
{
	private boolean bRun = true;
	private Resoure resoure;

	public Producter(Resoure resoure)
	{
		this.resoure = resoure;
	}

	@Override
	public void run()
	{
		int i = 0;
		String name;
		while (bRun)
		{
			if (i == 0)
			{
				name = "李雷+男";
			}
			else
			{
				name = "韩梅梅+女";
			}
			//System.out.println("Producter : " + name);
			synchronized (resoure)
			{
				resoure.put(name);
			}
			i = (i + 1) % 2;
		}
	}
}
Consumer类

/**
 * @author ZhengYesheng
 */
public class Consumer extends Thread
{
	private boolean bRun = true;
	private Resoure resoure;

	public Consumer(Resoure resoure)
	{
		this.resoure = resoure;
	}

	@Override
	public void run()
	{
		String name;
		while (bRun)
		{
			synchronized (resoure)
			{
				name=resoure.get();
			}
			System.out.println(name);
		}
	}
}
测试类Test_Priducter_Consumer

/**
 * @author ZhengYesheng
 *
 */
public class Test_Priducter_Consumer
{

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		Resoure resoure=new Resoure();
		Producter p=new Producter(resoure);
		Consumer c=new Consumer(resoure);
		p.start();
		c.start();
	}

}
xiaovhao 2017-08-09
  • 打赏
  • 举报
回复
比如你生产者走的是if,当消费者发出this.notify();会去唤醒生产者执行后续代码,而这时你的生产者都没有代码需要执行,因为你用的是if,else
三仙半 2017-08-09
  • 打赏
  • 举报
回复
这个可以按照生产者-消费者模式写
  • 打赏
  • 举报
回复
基础的问题啊: if else语句只走其一啊。走了if就不走else了啊。上面没有else。无论走不走if语句,他都要执行Thread.sleep(10); isExmpty=true; this.notify(); 下面你加了else,如果走了if语句,else中的this.name=name; Thread.sleep(10); this.gender=gender; isExmpty=false; this.notify();肯定不走了啊。结果当然不同啊
Lincain 2017-08-07
  • 打赏
  • 举报
回复
各位大神麻烦帮忙解释一下,不胜感觉啊

62,624

社区成员

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

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