單例模式詳解及java常用類

來源:互聯網
上載者:User

標籤:private   設計   date   a*   ==   日期   date()   zed   建構函式   

【單例模式】     確保某一個類,只能產生一個執行個體。設計思路:====將建構函式私人化,確保類外部,不能使用new關鍵字自行建立對象。====在類內部執行個體化一個對象,並通過靜態方法返回。(1)餓漢式單例模式優點:安全執行緒缺點:在類載入的時候,就執行個體化對象,提前佔用系統資源
 1 class Dog{ 2      3     private Dog(){ 4          5         System.out.println("小蠢狗"); 6     } 7      8     private static Dog cutedog=new Dog(); 9     10     public static Dog getInstanca(){11         return cutedog;12     }13     14 }
(2.)懶漢式單例模式:優點:解決了餓漢式單例,一載入就提前佔用資源的問題。缺點:線程不安全
 1 class Dog{ 2      3     private Dog(){ 4          5         System.out.println("小蠢狗"); 6     } 7      8     private static Dog cutedog=null; 9     10     public static Dog getInstanca(){11         if (cutedog==null) {12             cutedog=new Dog();13         }14         return cutedog;15     }16     17 }
(3.)線程鎖懶漢式模式通俗來說,就是在懶漢式單例模式的方法上加鎖synchronized:將一個方法或者代碼塊進行加鎖,同一時間只允許一個線程訪問使用同步塊對方法進行加鎖,確保懶漢式單例,可以安全執行緒缺點:效率低下
 1 class Dog{ 2      3     private Dog(){ 4          5         System.out.println("小蠢狗"); 6     } 7      8     private static Dog cutedog=null; 9     10     public static synchronized Dog getInstanca(){11         if (cutedog==null) {12             cutedog=new Dog();13         }14         return cutedog;15     }16     17 }

(4.)雙重加鎖懶漢模式:

只有第一次Dog為null時,才進行線程鎖,當後續Dog不為null時,說明第一次已經賦值了,·產生了一個執行個體,就無需線程鎖,可以允許多個線程同時拿走dog
class Dog{        private Dog(){                System.out.println("小蠢狗");    }        private static Dog cutedog=null;        public static synchronized Dog getInstanca(){        if (cutedog==null) {            synchronized (Dog.class) {                if (cutedog==null) {                    cutedog=new Dog();                }            }        }        return cutedog;    }    }
(5.)靜態內部類實現單例優點:解決了餓漢式提前佔用資源的問題,解決了懶漢式線程不安全的問題,靜態內部類只有當被調用的時候才開始首次被載入。
 1 class Dog{ 2      3     private Dog(){ 4          5         System.out.println("小蠢狗"); 6     } 7      8     private static class KIttyDod{ 9         private static Dog cutedog=new Dog();10     }11     public static Dog getInstance(){12         return KIttyDod.cutedog;13     }14     15 }
【Math】;?Math位於Java.lang包中(1.)Math.abs(); 求絕對值(2.)Math.cbrt(); 求立方根(3.)Math..sprt();求平方根(4.)Math.max/min(double1,double2); 返回兩個值中最大最小值(5.)Math.pow(a,b); 求a的b次方(6.)Math.floor();返回小於指定浮點數的一個整數,返回的是double類型,例如10.0Math.ceil();------大於---------(6.)Math.round();四捨五入,返回一個整數類型,如果傳入double,返回long,傳入float,返回int(7.)Math.rint();返回最接近參數的整數,如果10.5與10和11同時接近,返回偶數(8.)Math.random();返回[0.1,1.0)之間的double隨機值 【求一個數保留幾位小數】double a=23.22122222;double b=Math.round(a*Math.pow(10, 3))/Math.pow(10, 3);System.out.println(b); 【Date】/**執行個體化* 空參構造,預設取到目前時間*/Date date=new Date();Date d=new Date();/*** 傳入一個long類型的時間戳記,取到指定時間。* date.getTime(); 取到一個時間的時間戳記,從0時區,1970年1月1日0:0:0到目前時間毫秒數。*/System.out.println(d); (1.)equals();比對兩個時間是否相等(2.)a.before(b); 檢測一個時間是不是在制定時間之前after(): --------------------------------------之後(3.)a.compare to(b); 前面>參數 返回1 前面<小於參數,返回-1 相等,返回0(4.)setTime();傳入一個長整型(時間戳記),重新設定時間(5.)toString();格式化日期轉義格式yyyy-mm-dd。【SimpleDateFormat】對日期格式進行執行個體化。執行個體化對象時,傳入格式化參數,用字母代表對應部分。*  字母  日期或時間元素  表示  樣本   * y  年  Year  1996; 96   * M  年中的月份  Month  July; Jul; 07   * d  月份中的天數  Number  10   * E  星期中的天數  Text  Tuesday; Tue   * H  一天中的小時數(0-23)  Number  0   * h  am/pm 中的小時數(1-12)  Number  12   * m  小時中的分鐘數  Number  30   * s  分鐘中的秒數  Number  55  (1.)format();用於傳人一個Date類型的參數,並返回格式化之後的字串Date date=new Date();SimpleDateFormat sf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss E" );String s=sf.format(date);System.out.println(s);(2.)sf.parse("2018年04月13日 14:08:15 星期五");將字串格式的時間,轉成Date類型,要求傳入的字串格式,必須與執行個體化時的模式,保持一致。(3.)toPattern();返回當前格式化的模式字串。【Calendar日曆類】Calendar ca=Calendar.getInstance();(1.)Calendar日曆類是一個抽象類別,不能直接通過new拿到對象,必須使用Calendar.getInstance();拿到一個Calendar日曆類對象(2.)ca.getTime(); 返回一個日期對象(3.)ca.add(int field, int amount) ; 根據日曆的規則,將指定的時間量添加或減去給定的日曆欄位。【Random隨機數類】取隨機數,有兩種構造:--------------空參構造------------Random ran=new Random();--------------傳入一個種子數--------只要種子數相同,那麼在相同次數取到的隨機數肯定相同,這是偽隨機數。Random ran=new Random(100);Random ran=new Random(System.currentTimeMillis());System.currentTimeMillis();拿到目前時間戳,這樣就是取隨機數了。(1.)nextInt(); 隨機獲得一個整數(2.)nextInt(n); 隨機獲得從0到n隨機數,含0不含n隨機產生兩位隨機數: System.out.println(ran.nextInt(90)+10);

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

單例模式詳解及java常用類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.