| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 487 人关注过本帖
标题:哪位大神能帮我加下注释
只看楼主 加入收藏
编译中。。
Rank: 7Rank: 7Rank: 7
来 自:中国
等 级:黑侠
帖 子:198
专家分:511
注 册:2011-7-29
结帖率:75%
收藏
 问题点数:0 回复次数:1 
哪位大神能帮我加下注释
程序代码:
import java.awt.List;
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;


class Count {
    private int count = 0;
    private Random rand = new Random(47);

    public synchronized int increment() {
        int temp = count;
        if (rand.nextBoolean())
            Thread.yield();
        return (count = ++temp);
    }

    public synchronized int value() {
        return count;
    }

}

class Entrance implements Runnable {
    private static Count count = new Count();
    private static ArrayList<Entrance> entrances = new ArrayList<Entrance>();
    private int number = 0;

    private final int id;
    private static volatile boolean canceled = false;

    public static void cancel() {
        canceled = true;
    }

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

    public void run() {
        while (!canceled) {
            synchronized (this) {
                ++number;
            }
            System.out.println(this + "Total" + count.increment());
            try {
                TimeUnit.MICROSECONDS.sleep(100);
            } catch (InterruptedException e) {
                System.out.println("Sleep interrupted");
            }
        }
        System.out.println("Stopping" + this);

    }

    public synchronized int getValue() {
        return number;
    }

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

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

    public static int sumEntrances() {
        int sum = 0;
        for (Entrance entance : entrances) {
            sum += entance.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));
        // Run for a while, then stop and collect the data:
        TimeUnit.SECONDS.sleep(3);
        Entrance.cancel();
        exec.shutdown();
        if (!exec.awaitTermination(250, TimeUnit.MILLISECONDS))
            System.out.println("Some tasks were not terminated!");
        System.out.println("Total: " + Entrance.getTotalCount());
        System.out.println("Sum of Entrances: " + Entrance.sumEntrances());
    }
}
搜索更多相关主题的帖子: count 
2015-09-06 19:55
编译中。。
Rank: 7Rank: 7Rank: 7
来 自:中国
等 级:黑侠
帖 子:198
专家分:511
注 册:2011-7-29
收藏
得分:0 
我已经会了

 当我感到些许疲倦时   便想躺在阳光下,小路旁 . 可这些往往都是奢望..
2015-09-07 11:58
快速回复:哪位大神能帮我加下注释
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019334 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved