黑马程序员技术交流社区

标题: 帮忙分析以下代码,在多线程中,来运用懒汉式 [打印本页]

作者: 黑马黄文龙    时间: 2012-12-8 20:12
标题: 帮忙分析以下代码,在多线程中,来运用懒汉式
本帖最后由 黑马黄文龙 于 2012-12-8 23:19 编辑

帮忙分析以下代码,在多线程中,来运用懒汉式,然后看两个线程创建的对象是否为同一个对象,后面突然没思路了.......
  1. /*
  2. 需求:把单例模型运用到多线程中
  3. 分析:
  4. 1,创建一个单例模型
  5. 2,定义一个Test类,测试两个线程获得对象是不是同一个对象
  6. */
  7. //创建一个单例类
  8. class SingleOne_Thread
  9. {
  10.          
  11.         private static  SingleOne_Thread one;
  12.         private SingleOne_Thread(){}
  13.         //对外有一个公共访问方法来获得单例类的对象
  14.         public static SingleOne_Thread getOne()
  15.         {
  16.                
  17.                 if(one==null)
  18.                 {
  19.                         one=new SingleOne_Thread();
  20.                     return one;
  21.                 }
  22.         }
  23.         
  24. }
  25. //有实现Runnable接口的方式来创建线程,并且重写run方法
  26. class Thread_Test implements Runnable
  27. {
  28.         public void run()
  29.         {
  30.                         SingleOne_Thread.getOne();
  31.                         System.out.println(Thread.currentThread().getName());
  32.                
  33.         }
  34. }
  35. //定义一个Test类,来测试两个线程获得对象是不是同一个对象
  36. public class Test
  37. {
  38.         public static void main(String[]args)
  39.         {
  40.                 Runnable rt=new Thread_Test();
  41.                 Thread t1=new Thread(rt);
  42.                 Thread t2=new Thread(rt);

  43.                 t1.start();
  44.                 t2.start();
  45.         }
  46. }
复制代码

作者: 赵保磊    时间: 2012-12-8 22:07
/*
把单例模式应用到多线程中
*/


class singleElement
{
        private singleElement(){}
        private static singleElement se=null;
        public static singleElement getSe()
        {
                se=new singleElement();
                return se;
        }

}
class sorr implements Runnable
{
        public void run()
        {
                synchronized(sorr.class)
                        {
                         
             singleElement newSe=singleElement.getSe();
                         System.out.println(Thread.currentThread().getName()+":::"+newSe);
                        }
        }

}
class  aaaDemo
{
        public static void main(String[] args)
        {
               
                sorr s=new sorr();

                Thread t1=new Thread(s);
                Thread t2=new Thread(s);

                t1.start();
                t2.start();


        }


}



作者: xjandrew    时间: 2012-12-8 22:08
本帖最后由 xjandrew 于 2012-12-8 22:11 编辑
  1. class SingleOne_Thread
  2. {
  3.          
  4.         private static  SingleOne_Thread one;
  5.         private SingleOne_Thread(){}
  6.         //对外有一个公共访问方法来获得单例类的对象
  7.         public static SingleOne_Thread getOne()
  8.         {
  9.                
  10.                 if(one==null)
  11.                 {
  12.                         one=new SingleOne_Thread();
  13.                     return one;
  14.                 }
  15.                 else//这里出的问题,你仅有if情况下的返回值,当one!=null时怎办?(其实else可以不写)
  16.                         return null;//这里必须得返回个类型值,null是我随便加的,你依自己的需求来
  17.         }
  18.         
  19. }
复制代码

作者: xjandrew    时间: 2012-12-8 22:15
按2楼的例子来说,这样就行
  1. class SingleOne_Thread
  2. {
  3.          
  4.         private static  SingleOne_Thread one;
  5.         private SingleOne_Thread(){}
  6.         //对外有一个公共访问方法来获得单例类的对象
  7.         public static SingleOne_Thread getOne()
  8.         {
  9.                
  10.                 return new SingleOne_Thread();
  11.         }
  12.         
  13. }
复制代码

作者: 黑马黄文龙    时间: 2012-12-8 23:17
xjandrew 发表于 2012-12-8 22:08

谢谢!!!
作者: 黑马黄文龙    时间: 2012-12-8 23:17
赵保磊 发表于 2012-12-8 22:07
/*
把单例模式应用到多线程中
*/

谢谢!回答的很详细!!




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2