当前位置:   article > 正文

单例模式6种实现方式_写一个单例模式的例子,用代码实现

写一个单例模式的例子,用代码实现

一:饿汉式

  1. public class MyObject {
  2. // 立即加载方式==饿汉模式
  3. private static MyObject myObject = new MyObject();
  4. private MyObject() {
  5. }
  6. public static MyObject getInstance() {
  7. // 此代码版本为立即加载
  8. // 此版本代码的缺点是不能有其它实例变量
  9. // 因为getInstance()方法没有同步
  10. // 所以有可能出现非线程安全问题
  11. return myObject;
  12. }
  13. }
  1. public class MyThread extends Thread {
  2. @Override
  3. public void run() {
  4. System.out.println(MyObject.getInstance().hashCode());
  5. }
  6. }
  1. public static void main(String[] args) {
  2. MyThread t1 = new MyThread();
  3. MyThread t2 = new MyThread();
  4. MyThread t3 = new MyThread();
  5. t1.start();
  6. t2.start();
  7. t3.start();
  8. }

多线程获取单例,打印的hashcode是同一个值,说明对象是同一个 

二:懒汉式(1)

  1. public class MyObject {
  2. private static MyObject myObject;
  3. private MyObject() {
  4. }
  5. // 设置同步方法效率太低了
  6. // 整个方法被上锁
  7. synchronized public static MyObject getInstance() {
  8. try {
  9. if (myObject != null) {
  10. } else {
  11. // 模拟在创建对象之前做一些准备性的工作
  12. Thread.sleep(3000);
  13. myObject = new MyObject();
  14. }
  15. } catch (InterruptedException e) {
  16. e.printStackTrace();
  17. }
  18. return myObject;
  19. }
  20. }
  1. public class MyThread extends Thread {
  2. @Override
  3. public void run() {
  4. System.out.println(MyObject.getInstance().hashCode());
  5. }
  6. }
  1. public static void main(String[] args) {
  2. MyThread t1 = new MyThread();
  3. MyThread t2 = new MyThread();
  4. MyThread t3 = new MyThread();
  5. t1.start();
  6. t2.start();
  7. t3.start();
  8. }


二:懒汉式(2)


  1. public class MyThread extends Thread {
  2. @Override
  3. public void run() {
  4. System.out.println(MyObject.getInstance().hashCode());
  5. }
  6. }
  1. public class MyObject {
  2. private volatile static MyObject myObject;
  3. private MyObject() {
  4. }
  5. // 使用双检测机制来解决问题
  6. // 即保证了不需要同步代码的异步
  7. // 又保证了单例的效果
  8. public static MyObject getInstance() {
  9. try {
  10. if (myObject != null) {
  11. } else {
  12. // 模拟在创建对象之前做一些准备性的工作
  13. Thread.sleep(3000);
  14. synchronized (MyObject.class) {
  15. if (myObject == null) {
  16. myObject = new MyObject();
  17. }
  18. }
  19. }
  20. } catch (InterruptedException e) {
  21. e.printStackTrace();
  22. }
  23. return myObject;
  24. }
  25. // 此版本的代码称为:
  26. // 双重检查Double-Check Locking
  27. }
  1. public static void main(String[] args) {
  2. MyThread t1 = new MyThread();
  3. MyThread t2 = new MyThread();
  4. MyThread t3 = new MyThread();
  5. t1.start();
  6. t2.start();
  7. t3.start();
  8. }

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

  1. public class MyObject {
  2. // 内部类方式
  3. private static class MyObjectHandler {
  4. private static MyObject myObject = new MyObject();
  5. }
  6. private MyObject() {
  7. }
  8. public static MyObject getInstance() {
  9. return MyObjectHandler.myObject;
  10. }
  11. }
  1. public class MyThread extends Thread {
  2. @Override
  3. public void run() {
  4. System.out.println(MyObject.getInstance().hashCode());
  5. }
  6. }
  1. public static void main(String[] args) {
  2. MyThread t1 = new MyThread();
  3. MyThread t2 = new MyThread();
  4. MyThread t3 = new MyThread();
  5. t1.start();
  6. t2.start();
  7. t3.start();
  8. }

四:使用静态代码块实现单例模式

  1. public class MyObject {
  2. private static MyObject instance = null;
  3. private MyObject() {
  4. }
  5. static {
  6. instance = new MyObject();
  7. }
  8. public static MyObject getInstance() {
  9. return instance;
  10. }
  11. }
  1. public class MyThread extends Thread {
  2. @Override
  3. public void run() {
  4. for (int i = 0; i < 5; i++) {
  5. System.out.println(MyObject.getInstance().hashCode());
  6. }
  7. }
  8. }
  1. public static void main(String[] args) {
  2. MyThread t1 = new MyThread();
  3. MyThread t2 = new MyThread();
  4. MyThread t3 = new MyThread();
  5. t1.start();
  6. t2.start();
  7. t3.start();
  8. }

五:使用枚举实现单例模式

  1. public enum MyObject {
  2. connectionFactory;
  3. private Connection connection;
  4. private MyObject() {
  5. try {
  6. System.out.println("调用了MyObject的构造");
  7. String url = "jdbc:sqlserver://localhost:1079;databaseName=ghydb";
  8. String username = "sa";
  9. String password = "";
  10. String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
  11. Class.forName(driverName);
  12. connection = DriverManager.getConnection(url, username, password);
  13. } catch (ClassNotFoundException e) {
  14. e.printStackTrace();
  15. } catch (SQLException e) {
  16. e.printStackTrace();
  17. }
  18. }
  19. public Connection getConnection() {
  20. return connection;
  21. }
  22. }
  1. public class MyThread extends Thread {
  2. @Override
  3. public void run() {
  4. for (int i = 0; i < 5; i++) {
  5. System.out.println(MyObject.connectionFactory.getConnection()
  6. .hashCode());
  7. }
  8. }
  9. }
  1. public static void main(String[] args) {
  2. MyThread t1 = new MyThread();
  3. MyThread t2 = new MyThread();
  4. MyThread t3 = new MyThread();
  5. t1.start();
  6. t2.start();
  7. t3.start();
  8. }


六:序列化与反序列化的单例模式实现

  1. public class MyObject implements Serializable {
  2. private static final long serialVersionUID = 888L;
  3. // 内部类方式
  4. private static class MyObjectHandler {
  5. private static final MyObject myObject = new MyObject();
  6. }
  7. private MyObject() {
  8. }
  9. public static MyObject getInstance() {
  10. return MyObjectHandler.myObject;
  11. }
  12. protected Object readResolve() throws ObjectStreamException {
  13. System.out.println("调用了readResolve方法!");
  14. return MyObjectHandler.myObject;
  15. }
  16. }
  1. public class SaveAndRead {
  2. public static void main(String[] args) {
  3. try {
  4. MyObject myObject = MyObject.getInstance();
  5. FileOutputStream fosRef = new FileOutputStream(new File(
  6. "myObjectFile.txt"));
  7. ObjectOutputStream oosRef = new ObjectOutputStream(fosRef);
  8. oosRef.writeObject(myObject);
  9. oosRef.close();
  10. fosRef.close();
  11. System.out.println(myObject.hashCode());
  12. } catch (FileNotFoundException e) {
  13. // TODO Auto-generated catch block
  14. e.printStackTrace();
  15. } catch (IOException e) {
  16. // TODO Auto-generated catch block
  17. e.printStackTrace();
  18. }
  19. try {
  20. FileInputStream fisRef = new FileInputStream(new File(
  21. "myObjectFile.txt"));
  22. ObjectInputStream iosRef = new ObjectInputStream(fisRef);
  23. MyObject myObject = (MyObject) iosRef.readObject();
  24. iosRef.close();
  25. fisRef.close();
  26. System.out.println(myObject.hashCode());
  27. } catch (FileNotFoundException e) {
  28. e.printStackTrace();
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. } catch (ClassNotFoundException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. }




本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号