不正确的访问共享资源

看下面一段代码,这段代码采用了模板设计模式

public abstract class IntGenerator {
private volatile boolean canceled=false;

public abstract int next();

public void cancel()
{ this.canceled=false;
}

public boolean isCanceled()
{
return this.canceled;
}


}
========================
//创建一个偶数生成类
public class EvenGenerator extends IntGenerator{

private int currentEvenValue=0;

@Override
public int next() {
// TODO Auto-generated method stub
++currentEvenValue;
++currentEvenValue;

return currentEvenValue;
}

public static void main(String[] args)
{
EvenChecker.test(new EvenGenerator());
}


}
===========================
创建一个测试类,启动10个线程,查看是否正确的访问资源
public class EvenChecker implements Runnable{
private IntGenerator generator;
private final int id;

public EvenChecker(IntGenerator generator,int id) {
// TODO Auto-generated constructor stub
this.generator=generator;
this.id=id;
}

@Override
public void run() {
// TODO Auto-generated method stub
while(!generator.isCanceled())
{
int val=generator.next();
if(val%2!=0)
{
System.out.println(val+ " is not even!");

generator.cancel();//直到找到奇数时停止
}
else
{
//System.out.println(val+" is even!");
}
}

}



public static void test(IntGenerator gp,int count)
{

System.out.println("Press Ctrl+C to Exit");
ExecutorService exec=Executors.newCachedThreadPool();
for(int i=0;i<count;i++)
exec.execute(new EvenChecker(gp,i));
}


public static void test(IntGenerator gp)
{
test(gp,10);
}




}

//测试的结果是能打印出很多奇数,访问资源出错的原因是:next()方法本身不是原子方法,可能在一个线程对它进行第一个++操作之后,第二个线程已经取到它的值了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值