本帖最后由 HM何伟 于 2013-4-2 00:25 编辑
这段代码有安全问题没??- class Single{
- private Single(){}
- private static Single s=null;
- public static Single getOop()
- {
- synchronized(Single.class)
- {
- if (s==null)//?????????
-
- try
- {
- Thread.sleep(20);
- }
- catch (Exception e)
- {
- }
-
- s=new Single();
- return s;
- }
- //return s;
- }
- }
- class Test implements Runnable{
- public void run()
- {
- while(true)
- {
- Single s = Single.getOop();
- System.out.println(s);
- }
- }
- }
- class Main{
- public static void main(String[] args){
- Test t=new Test();
- Thread t1=new Thread(t);
- Thread t2=new Thread(t);
- Thread t3=new Thread(t);
- t1.start();
- t2.start();
- t3.start();
-
- }
- }
复制代码 |