码迷,mamicode.com
首页 > 其他好文 > 详细

有意思的登记式(乱入懒汉恶汉)

时间:2017-08-14 22:24:38      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:有意思的登记式(乱入懒汉恶汉)

不多哔哔 只是一些小玩应,初学勿喷 希望能学到点东西


import java.util.HashMap;

import java.util.Map;

import java.util.Set;


public class AloneDemo {

public static void main(String[] args)

{

CheckDemo cd1 = CheckDemo.getInstance(3);

//sopln(cd1.getCount());

CheckDemo cd2 = CheckDemo.getInstance(3);

//sopln(cd2.getCount());

CheckDemo cd4 = CheckDemo.getInstance(3);

//sopln(cd4.getCount());

CheckDemo cd5 = CheckDemo.getInstance(3);

//sopln(cd5.getCount());

CheckDemo cd3 = CheckDemo.getInstance(3);

//sopln(cd3.getCount());

CheckDemo cd6 = CheckDemo.getInstance(3);

//sopln(cd6.getCount());

}

public static void sopln(Object obj)

{

System.out.println(obj.toString());

}

}

class HungryMan

{

private HungryMan hm = new HungryMan();

private HungryMan()

{

}

public HungryMan getInstance()

{

return hm;

}

}

class LazyMan

{

private LazyMan lm;

private LazyMan()

{

}

public LazyMan getInstance()

{

if(lm == null)

lm = new LazyMan();

return lm;

}

}

class CheckDemo

{

private static Map<Integer,CheckDemo> m = new HashMap();

static int count = -1;

private CheckDemo()

{

sopln("--"+count);

}

        //输入最多能创建的实例,

public static CheckDemo getInstance(int i)

{

if(++count<i)

{

m.put(count, new CheckDemo());

int ran = (int)(Math.random()*count);

sopln(ran);

return m.get(ran);

// return m.get((int)(Math.random()*count));

}

else

{

int ran = (int)(Math.random()*i);

sopln(ran);

return m.get(ran);

// return m.get((int)(Math.random()*i));

}

}

public static void sopln(Object obj)

{

System.out.println(obj.toString());

}

public int getCount()

{

return count;

}

}


有意思的登记式(乱入懒汉恶汉)

标签:有意思的登记式(乱入懒汉恶汉)

原文地址:http://13215514.blog.51cto.com/13205514/1956242

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!