Java多线程通信之----------男女混合双打

package ParentsTeachChild;

public class Test
{
	public static void main(String[] args)
	{
		Child child = new Child("小明", true);
		Father father = new Father("望子成龙的爸爸", child);
		Mother mother = new Mother("溺爱的妈妈", child);
		Thread fThread = new Thread(father);
		Thread mThread = new Thread(mother);
		fThread.setDaemon(true);
		fThread.start();
		mThread.start();
	}
}

package ParentsTeachChild;

public class Child
{
	private int blood;
	private String name;
	private boolean isNaughty;

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public boolean isNaughty()
	{
		return isNaughty;
	}

	public void setNaughty(boolean isNaughty)
	{
		this.isNaughty = isNaughty;
	}

	public Child(String name, boolean isNaughty)
	{
		super();
		this.name = name;
		this.isNaughty = isNaughty;
		System.out
				.println("==================================================");
		System.out
				.println("                                                                   "
						+ this.name + "很调皮!");
		System.out
				.println("==================================================");
		this.blood = 100;
	}

	public int getBlood()
	{
		return blood;
	}

	public void setBlood(int blood)
	{
		this.blood = blood;
	}
}

package ParentsTeachChild;

public class Father implements Runnable
{
	private String fName;
	private Child child;

	public String getfName()
	{
		return fName;
	}

	public void setfName(String fName)
	{
		this.fName = fName;
	}

	public Child getChild()
	{
		return child;
	}

	public void setChild(Child child)
	{
		this.child = child;
	}

	public Father(String fName, Child child)
	{
		super();
		this.fName = fName;
		this.child = child;
	}

	public void edu()
	{
		synchronized (child)
		{
			if (child.isNaughty())
			{
				System.out.println(this.fName + ":我来教育");
				child.setBlood(child.getBlood() - 2);
				System.out.println(child.getName() + ": 555...555.....疼死了!");
				child.setNaughty(false);
				System.out.println(child.getName()+":"+child.getBlood());
				child.notify();
				try
				{
					child.wait();
				}
				catch (InterruptedException e)
				{
					e.printStackTrace();
				}
			}
			else
			{
				try
				{
					child.wait();
				}
				catch (InterruptedException e)
				{
					e.printStackTrace();
				}
			}
		}
	}

	@Override
	public void run()
	{
		while (child.getBlood() >0)
		{
			edu();
		}
		System.out.println("儿呀,是爸的错,爸把你害死了!");
	}
}

package ParentsTeachChild;

public class Mother implements Runnable
{
	private String mName;
	private Child child;

	public String getmName()
	{
		return mName;
	}

	public void setmName(String mName)
	{
		this.mName = mName;
	}

	public Child getChild()
	{
		return child;
	}

	public void setChild(Child child)
	{
		this.child = child;
	}

	public Mother(String mName, Child child)
	{
		super();
		this.mName = mName;
		this.child = child;
	}

	public void edu()
	{
		synchronized (child)
		{
			if (!child.isNaughty())
			{
				System.out.println(this.mName + ":孩子不疼!");
				System.out
						.println(child.getName() + ":" + this.mName + " 好爱我!");
				child.setBlood(child.getBlood() + 1);
				child.setNaughty(true);
				System.out.println(child.getName() + ":" + child.getBlood());
				if (child.getBlood() > 0)
				{
					System.out.println(child.getName() + "调皮了一下");
				}
				child.notify();
				try
				{
					child.wait();
				}
				catch (InterruptedException e)
				{
					e.printStackTrace();
				}
			}
			else
			{
				try
				{
					child.wait();
				}
				catch (InterruptedException e)
				{
					e.printStackTrace();
				}
			}
		}
	}

	@Override
	public void run()
	{
		while (child.getBlood() > 0)
		{
			edu();
		}
		System.out.println(this.mName + " : 我儿死的好惨!");
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值