定义了一个单例全局资源Flower,想每次到JiaBaoYu处理的时候leaf_num--,到leaf_num为0的时候程序结束,但每次Flower都等于是重新设置了一次,这是运行结果:
JiaMu passed
5
JiaShe passed
5
JiaZheng passed
5
5
JiaBaoYu passed
JiaMu passed
5
JiaShe passed
5
JiaZheng passed
5
JiaBaoYu gonna drink
drum stopped
package com.test.beater;import java.util.Timer;
import java.util.TimerTask;
import com.test.flower.Flower;
import com.test.player.JiaBaoYu;
import com.test.player.JiaMu;
import com.test.player.JiaShe;
import com.test.player.JiaZheng;public class DrumBeater {
private static boolean stopped = false;
private Timer timer;
public static boolean isStopped() {
return stopped;
} public static void setStopped(boolean stopped) {
DrumBeater.stopped = stopped;
} public static void main(String [] args)
{
DrumBeater drumbeater = new DrumBeater();
drumbeater.beating(1);
JiaMu jiamu = new JiaMu();
JiaShe jiashe = new JiaShe();
JiaZheng jiazheng = new JiaZheng();
JiaBaoYu jiabaoyu = new JiaBaoYu();
Flower.getInstance().setLeaf_num(5);
jiamu.setSuccessor(jiashe);
jiashe.setSuccessor(jiazheng);
jiazheng.setSuccessor(jiabaoyu);
jiabaoyu.setSuccessor(jiamu);
// DrumBeater.setLeaf_num(3); // System.out.println(Flower.getInstance()==Flower.getInstance());
jiamu.handle();
}
public void beating(int i)
{
timer = new Timer();
timer.schedule(new BeatDrum(), 10*i);
}
class BeatDrum extends TimerTask
{
@Override
public void run() {
// TODO Auto-generated method stub
DrumBeater.setStopped(true);
System.out.println("drum stopped");
timer.cancel();
}
}
}package com.test.flower;import com.test.beater.DrumBeater;public class Flower {
private static int leaf_num ;
private static Flower flower = null;
private Flower()
{
}
public static Flower getInstance()
{
if(flower == null)
{
flower = new Flower();
}
return flower;
}
public int getLeaf_num() {
return leaf_num;
} public synchronized void setLeaf_num(int leaf_num1) {
Flower.leaf_num = leaf_num1;
}}package com.test.flower;import com.test.beater.DrumBeater;public class Flower {
private static int leaf_num ;
private static Flower flower = null;
private Flower()
{
}
public static Flower getInstance()
{
if(flower == null)
{
flower = new Flower();
}
return flower;
}
public int getLeaf_num() {
return leaf_num;
} public synchronized void setLeaf_num(int leaf_num1) {
Flower.leaf_num = leaf_num1;
}}package com.test.player;public abstract class Player {
private Player successor;
public abstract void handle();
public void next()
{
if(successor != null)
{
successor.handle();
}
else
{
System.out.println("program terminate");
System.exit(0);
}
}
public void setSuccessor(Player successor)
{
this.successor = successor;
}
}package com.test.player;import com.test.beater.DrumBeater;
import com.test.flower.Flower;public class JiaMu extends Player {
@Override
public void handle() {
// TODO Auto-generated method stub
if(DrumBeater.isStopped())
{
System.out.println("JiaMu gonna drink");
}
else
{
System.out.println("JiaMu passed");
System.out.println(Flower.getInstance().getLeaf_num());
next();
}
}}
package com.test.player;import com.test.beater.DrumBeater;
import com.test.flower.Flower;public class JiaShe extends Player { @Override
public void handle() {
// TODO Auto-generated method stub
if(DrumBeater.isStopped())
{
System.out.println("JiaShe gonna drink");
}
else
{
System.out.println("JiaShe passed");
System.out.println(Flower.getInstance().getLeaf_num());
next();
}
}}
package com.test.player;import com.test.beater.DrumBeater;
import com.test.flower.Flower;public class JiaZheng extends Player { @Override
public void handle() {
// TODO Auto-generated method stub
if(DrumBeater.isStopped())
{
System.out.println("JiaZheng gonna drink");
}
else
{
System.out.println("JiaZheng passed");
System.out.println(Flower.getInstance().getLeaf_num());
next();
}
}}
JiaMu passed
5
JiaShe passed
5
JiaZheng passed
5
5
JiaBaoYu passed
JiaMu passed
5
JiaShe passed
5
JiaZheng passed
5
JiaBaoYu gonna drink
drum stopped
package com.test.beater;import java.util.Timer;
import java.util.TimerTask;
import com.test.flower.Flower;
import com.test.player.JiaBaoYu;
import com.test.player.JiaMu;
import com.test.player.JiaShe;
import com.test.player.JiaZheng;public class DrumBeater {
private static boolean stopped = false;
private Timer timer;
public static boolean isStopped() {
return stopped;
} public static void setStopped(boolean stopped) {
DrumBeater.stopped = stopped;
} public static void main(String [] args)
{
DrumBeater drumbeater = new DrumBeater();
drumbeater.beating(1);
JiaMu jiamu = new JiaMu();
JiaShe jiashe = new JiaShe();
JiaZheng jiazheng = new JiaZheng();
JiaBaoYu jiabaoyu = new JiaBaoYu();
Flower.getInstance().setLeaf_num(5);
jiamu.setSuccessor(jiashe);
jiashe.setSuccessor(jiazheng);
jiazheng.setSuccessor(jiabaoyu);
jiabaoyu.setSuccessor(jiamu);
// DrumBeater.setLeaf_num(3); // System.out.println(Flower.getInstance()==Flower.getInstance());
jiamu.handle();
}
public void beating(int i)
{
timer = new Timer();
timer.schedule(new BeatDrum(), 10*i);
}
class BeatDrum extends TimerTask
{
@Override
public void run() {
// TODO Auto-generated method stub
DrumBeater.setStopped(true);
System.out.println("drum stopped");
timer.cancel();
}
}
}package com.test.flower;import com.test.beater.DrumBeater;public class Flower {
private static int leaf_num ;
private static Flower flower = null;
private Flower()
{
}
public static Flower getInstance()
{
if(flower == null)
{
flower = new Flower();
}
return flower;
}
public int getLeaf_num() {
return leaf_num;
} public synchronized void setLeaf_num(int leaf_num1) {
Flower.leaf_num = leaf_num1;
}}package com.test.flower;import com.test.beater.DrumBeater;public class Flower {
private static int leaf_num ;
private static Flower flower = null;
private Flower()
{
}
public static Flower getInstance()
{
if(flower == null)
{
flower = new Flower();
}
return flower;
}
public int getLeaf_num() {
return leaf_num;
} public synchronized void setLeaf_num(int leaf_num1) {
Flower.leaf_num = leaf_num1;
}}package com.test.player;public abstract class Player {
private Player successor;
public abstract void handle();
public void next()
{
if(successor != null)
{
successor.handle();
}
else
{
System.out.println("program terminate");
System.exit(0);
}
}
public void setSuccessor(Player successor)
{
this.successor = successor;
}
}package com.test.player;import com.test.beater.DrumBeater;
import com.test.flower.Flower;public class JiaMu extends Player {
@Override
public void handle() {
// TODO Auto-generated method stub
if(DrumBeater.isStopped())
{
System.out.println("JiaMu gonna drink");
}
else
{
System.out.println("JiaMu passed");
System.out.println(Flower.getInstance().getLeaf_num());
next();
}
}}
package com.test.player;import com.test.beater.DrumBeater;
import com.test.flower.Flower;public class JiaShe extends Player { @Override
public void handle() {
// TODO Auto-generated method stub
if(DrumBeater.isStopped())
{
System.out.println("JiaShe gonna drink");
}
else
{
System.out.println("JiaShe passed");
System.out.println(Flower.getInstance().getLeaf_num());
next();
}
}}
package com.test.player;import com.test.beater.DrumBeater;
import com.test.flower.Flower;public class JiaZheng extends Player { @Override
public void handle() {
// TODO Auto-generated method stub
if(DrumBeater.isStopped())
{
System.out.println("JiaZheng gonna drink");
}
else
{
System.out.println("JiaZheng passed");
System.out.println(Flower.getInstance().getLeaf_num());
next();
}
}}
解决方案 »
- 关于Map集合
- 问一个关于JAVA 线程throws InterruptedException 的基础问题
- 关于vector的问题,求助!!!!!!!!!
- 用java程序将GBK字符转成UTF-8编码格式的代码
- java 与 windows NT 的兼容性问题?
- 请问怎么样将URL后面的参数隐藏传递?
- 【】
- byte[] 到float的转换问题!急!!!
- 我是新手,请问Jbuilder中的控件怎样调整大小和位置,请大虾多多指点
- 高分,不够再加!!如何用java applet实现在客户端浏览器中显示即时的web server的系统时间
- Excel宏操作
- 2010支付宝西安最新笔试题..求高手给出算法和思路
private static int leaf_num = 10;
private static Flower flower = null;
private Flower()
{
}
public static Flower getInstance()
{
if(flower == null)
{
flower = new Flower();
}
return flower;
}
public int getLeaf_num() {
return leaf_num;
} public synchronized void deLeaf_num()
{
leaf_num--;
}}