/**
* 资源共享的示例
* 装饰性花园仿真程序
* 花园委员会希望知道每天通过多个大门进入公园的总人数
* 每个大门都有一个十字专转门或某种形式的计数器,
* 并且任何一个十字转门的计数值递增时,
* 就表示公园的总人数的共享计数值也会递增
*
* @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());
}
}
共享资源的简单示例
最新推荐文章于 2021-07-22 05:54:14 发布
1136

被折叠的 条评论
为什么被折叠?



