多线程访问全局变量
定义了一个单例全局资源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();
}
}
} --------------------编程问答-------------------- 不知道你问的是什么问题!跟你说下基本的概念吧
如果一个变量是成员变量,那么多个线程对同一个对象的成员变量进行操作时。他们对该成员变量是彼此影响的(也就是说一个线程对成员变量的修改会影响到另外的一个线程) --------------------编程问答-------------------- 你的问题是什么呢,如果是线程访问公共资源的问题,你可以加synchronise修饰,或者单个资源问题 ,推荐使用threadlocal类。 --------------------编程问答-------------------- --------------------编程问答-------------------- 当时的问题是多个线程访问后公共资源量没有减少,不过我已经找到原因了,我以为是多线程访问出错了,但不是线程方面的问题,是leafnum减少的函数应该放在公共资源Flower 里而不是付给处理请求的类。 --------------------编程问答-------------------- package com.test.flower;
import com.test.beater.DrumBeater;
public class Flower {
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--;
}
}
补充:Java , Java SE