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 + " : 我兒死的好慘!");
}
}