不正确的访问共享资源

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

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()方法本身不是原子方法,可能在一个线程对它进行第一个++操作之后,第二个线程已经取到它的值了

你可能感兴趣的:(资源)