• 单例模式


    java单例模式实现

     1 package chapter02;
     2 
     3 /*
     4  * 使用双检测机制,既保证了不需要同步代码的异步性,又保证了单例的效果
     5  */
     6 public class MyObject {
     7     private volatile static MyObject myobject;
     8     
     9     private MyObject() {
    10         
    11     }
    12     
    13     public static MyObject getInstance() {
    14         
    15         try {
    16             if(myobject != null) {
    17                 
    18             }else {
    19                 Thread.sleep(3000);//模拟在生成对象之前的一些准备性工作
    20                 synchronized (MyObject.class) {
    21                     if(myobject == null) {
    22                         myobject = new MyObject();
    23                     }
    24                 }
    25             }
    26         } catch (InterruptedException e) {
    27             // TODO: handle exception
    28             e.printStackTrace();
    29         }
    30         
    31         return myobject;
    32     }
    33 }
     1 package chapter02;
     2 
     3 public class MyThread extends Thread {
     4 
     5     @Override
     6     public void run() {
     7         // TODO Auto-generated method stub
     8         System.out.println(MyObject.getInstance().hashCode());
     9     }
    10 }
    package chapter02;
    
    public class Run {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            MyThread t1 = new MyThread();
            MyThread t2 = new MyThread();
            t1.start();
            t2.start();
        }
    
    }

    使用静态内置类实现单例模式

    package chapter03;
    
    public class MyObject {
        //内部类实现
        private static class MyObjectHandler{
            private static MyObject myobejct = new MyObject();
        }
        
        public MyObject() {
            
        }
        
        public static MyObject getInstance() {
            return MyObjectHandler.myobejct;
        }
    }
     1 package chapter03;
     2 
     3 public class MyThread extends Thread {
     4 
     5     @Override
     6     public void run() {
     7         // TODO Auto-generated method stub
     8         System.out.println(MyObject.getInstance().hashCode());
     9     }
    10 
    11     
    12 }
     1 package chapter03;
     2 
     3 
     4 public class Run {
     5 
     6     public static void main(String[] args) {
     7         // TODO Auto-generated method stub
     8         MyThread t1 = new MyThread();
     9         MyThread t2 = new MyThread();
    10         t1.start();
    11         t2.start();
    12     }
    13 
    14 }

    使用static代码块实现单例模式

    package chapter04;
    
    public class MyObject {
        private static MyObject instance = null;
        
        private MyObject() {
            
        }
        
        static {
            instance = new MyObject();
        }
        
        public static MyObject getInstance() {
            return instance;
        }
        
    }
    package chapter04;
    
    public class MyThread extends Thread {
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
            for (int i = 0; i < 5; i++) {
                System.out.println(MyObject.getInstance().hashCode());
            }
        }
        
    }
    package chapter04;
    
    public class Run {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            MyThread t1 = new MyThread();
            MyThread t2 = new MyThread();
            
            t1.start();
            t2.start();
        }
    
    }

    使用enum枚举数据类型实现单例模式

    package chapter05;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    public class MyObject {
        public enum MyEnumSingleton{
            connectionFactory;
            private Connection connection;
            private MyEnumSingleton() {
                try {
                    System.out.println("创建MyObject对象");
                    String url = "jdbc:sqlserver://localhost:1079;databaseName=y2";
                    String username = "sa";
                    String password = "";
                    String drivename = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
                    Class.forName(drivename);
                    connection = DriverManager.getConnection(url, username, password);
                    
                    
                } catch (ClassNotFoundException e) {
                    // TODO: handle exception
                    e.printStackTrace();
                } catch (SQLException e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }
            }
            
            public Connection getConnection() {
                return connection;
            }
        }
        
        public static Connection getConnection() {
            return MyEnumSingleton.connectionFactory.getConnection();
        }
    }
    package chapter05;
    
    public class MyThread extends Thread {
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
            for (int i = 0; i < 5; i++) {
                System.out.println(MyObject.getConnection().hashCode());
            }
        }
        
    }
  • 相关阅读:
    github上的每日学习 13
    github上的每日学习 12
    github上的每日学习 11
    github上的每日学习 10
    github上的每日学习 9
    github上的每日学习 8
    github上的每日学习 7
    面向对象程序设计寒假作业2
    MySQL安装和配置
    Fast Packet Processing with eBPF and XDP部分
  • 原文地址:https://www.cnblogs.com/wylwyl/p/10227320.html
Copyright © 2020-2023  润新知