共享资源的简单示例

/**
 * 资源共享的示例
 * 装饰性花园仿真程序
 * 花园委员会希望知道每天通过多个大门进入公园的总人数 
 * 每个大门都有一个十字专转门或某种形式的计数器,
 * 并且任何一个十字转门的计数值递增时,
 * 就表示公园的总人数的共享计数值也会递增
 * 
 * @author cxp
 * @data 2016-08-29
 * @modify
 * 
 */
class Count {
    private int count = 0;
    private Random rand = new Random(47);// 47为随机数生成器的种子,默认将当前时间作为随机数的种子

    // 移除synchronized关键字,程序将会失败
    public synchronized int increment() {
        int temp = count;
        if (rand.nextBoolean())// 把count读取到temp中到递增temp并将其存储回count的这段时间里,大约一半的时间产生让步
            Thread.yield();
        return (count = ++temp);
    }

    public synchronized int value() {
        return count;
    }
}

class Entrance implements Runnable {
    private static Count count = new Count();
    private static List<Entrance> entrances = new ArrayList<Entrance>();
    private int number = 0;
    // 不需要同步对其的访问
    private final int id;
    private static volatile boolean canceled = false;

    // 在volatile域里是原子操作
    public static void cancel() {
        canceled = true;
    }

    public Entrance(int id) {
        this.id = id;
        entrances.add(this);
    }

    @Override
    public void run() {
        while (!canceled) {
            synchronized (this) {
                number++;
            }
            print(this + " Total:" + count.increment());
            try {
                TimeUnit.MILLISECONDS.sleep(100);
            } catch (InterruptedException e) {
                print("sleep interrupted");
            }
        }
        print("Stopping" + this);
    }

    public synchronized int getValue() {
        return number;
    }

    @Override
    public String toString() {
        return "Entrance " + id + ": " + getValue();
    }

    public static int getTotalCount() {
        return count.value();
    }

    public static int sumEntrances() {
        int sum = 0;
        for (Entrance entrance : entrances)
            sum += entrance.getValue();
        return sum;
    }
}

public class OrnamentalGarden {
    public static void main(String[] args) throws Exception {
        ExecutorService exec = Executors.newCachedThreadPool();
        for (int i = 0; i < 5; i++)
            exec.execute(new Entrance(i));
        // 运行一段时间,然后停止和收集数据
        TimeUnit.SECONDS.sleep(3);// 当前线程睡眠3秒
        Entrance.cancel();// 发送cancel()消息
        exec.shutdown();
        if (!exec.awaitTermination(250, TimeUnit.MILLISECONDS))//等待每个任务结束,在超时时间达到之前全部结束返回true,否则返回false
            print("Some task were not terminated");
        print("Total:" + Entrance.getTotalCount());
        print("Sum of Entrances:" + Entrance.sumEntrances());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值