满意答案
backy695
2015.04.28
采纳率:44% 等级:9
已帮助:467人
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748/** * * @author Administrator * */public class Counter {\x05\x05//单例模式\x05private static Counter uniqueCounter = null;\x05 \x05private Counter() {\x05 // Exists only to defeat instantiation.\x05}\x05//获取实例\x05public synchronized static Counter getInstance() {\x05 if (uniqueCounter == null) {\x05\x05 uniqueCounter = new Counter();\x05 }\x05 return uniqueCounter;\x05}\x05//计数\x05private static int count = 0;\x05/**\x05 * 获取计数 \x05 * @return\x05 */\x05public static int getCount() {\x05\x05return count;\x05}\x05/**\x05 * 互斥访问\x05 * 增加偏移量\x05 * @param offset\x05 */\x05public synchronized static void add (int offset) {\x05\x05count = count + offset;\x05}\x05/**\x05 * 互斥访问\x05 * 减少偏移量\x05 * @param offset\x05 */\x05public synchronized static void sub (int offset) {\x05\x05count = count - offset;\x05} }访问的时候用Counter.getInstance()获取实例.
04分享举报