zoukankan      html  css  js  c++  java
  • java懒汉式单例遇到多线程

    单例模式确保某个类只有一个实例,而且自行实例化并向整个系统提供这个实例。

    在计算机系统中,线程池、缓存、日志对象、对话框、打印机、显卡的驱动程序对象常被设计成单例。这些应用都或多或少具有资源管理器的功能。

    每台计算机可以有若干个打印机,但只能有一个Printer Spooler,以避免两个打印作业同时输出到打印机中。每台计算机可以有若干通信端口,系统应当集中管理这些通信端口,以避免一个通信端口同时被两个请求同时调用。总之,选择单例模式就是为了避免不一致状态,避免政出多头。

     1 public class SingletonThread implements Runnable {
     2     private User u = null;
     3 
     4     public static void main(String[] args) throws InterruptedException {
     5         for(int i=0;i<1000;i++){
     6             SingletonThread t  = new SingletonThread();
     7             SingletonThread t1 = new SingletonThread();
     8             Thread t3 = new Thread(t);
     9             Thread t4 = new Thread(t1);
    10             t3.start();
    11             t4.start();
    12             t3.join(0);
    13             t4.join(0);
    14             if(t.getUser()!=t1.getUser())
    15             System.out.println("多线程引起单例创建失败...");
    16         }
    17         
    18     }
    19     public User getUser() {
    20         return u;
    21     }
    22 
    23     @Override
    24     public void run() {
    25         try {
    26             u = User.getInstance();
    27         } catch (InterruptedException e) {
    28             // TODO Auto-generated catch block
    29             e.printStackTrace();
    30         }
    31     }
    32     /*
    33      * 懒汉式单例--线程不安全
    34      * */
    35     public static class User {
    36         private User() {
    37         }
    38 
    39         private static User u = null;
    40 
    41         public static User getInstance() throws InterruptedException {
    42             if (u == null) {
    43                 Thread.currentThread().sleep(1000);
    44                 System.out.println("threadId:" + Thread.currentThread().getId());
    45                 u = new User();
    46             }
    47             return u;
    48         }
    49     }
    50     /*
    51      * 恶汉式单例--线程安全
    52      * 
    53      * */
    54     public static class User1 {
    55         private User1() {}
    56         private static User1 u = new User1();
    57 
    58         public static User1 getInstance() throws InterruptedException {
    59             Thread.currentThread().sleep(1000);
    60             return u;
    61         }
    62     }
    63 
    64 }
  • 相关阅读:
    1150 Travelling Salesman Problem(25 分)
    poj 2408 Anagram Groups
    guava学习--ratelimiter
    guava学习--Objects
    guava学习--ComparisonChain
    guava学习--Preconditions
    guava学习--Function、Predicate
    guava学习--FutureFallback
    guava学习--FutureCallback
    guava学习--SettableFuture
  • 原文地址:https://www.cnblogs.com/oxf5deb3/p/6218829.html
Copyright © 2011-2022 走看看