十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
單例模式1:
站在用戶的角度思考問題,與客戶深入溝通,找到紅山網(wǎng)站設(shè)計(jì)與紅山網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、國(guó)際域名空間、虛擬空間、企業(yè)郵箱。業(yè)務(wù)覆蓋紅山地區(qū)。
public
class
singleton{
private
static
singleton
st
=
null;
private
singleton(){
}
public
static
singleton
getinstance(){
if(st
==
null){
st
=
new
singleton();
}
return
st;
}
}
單例模式2:
public
class
singleton{
private
static
singleton
st
=
new
singleton();
private
singleton(){
}
public
static
singleton
getinstance(){
return
st;
}
}
多線程1:
導(dǎo)入thread所在的包
public
class
mythread1
extends
thread{
public
void
run(){
xxxxx寫自己的代碼
}
}
多線程2
導(dǎo)入runnable所在的包
public
class
mythread2
implements
runnable{
public
void
run(){
xxxxx寫自己的代碼
}
}
另寫一個(gè)測(cè)試類,在main方法中這樣寫:
thread
t
=
new
mythread1();
或者
runnable
r
=
new
mythread2();
thread
t
=
new
thread(r);
java中單例模式是一種常見的設(shè)計(jì)模式,單例模式分三種:懶漢式單例、餓漢式單例、登記式單例三種。
單例模式有一下特點(diǎn):
1、單例類只能有一個(gè)實(shí)例。
2、單例類必須自己自己創(chuàng)建自己的唯一實(shí)例。
3、單例類必須給所有其他對(duì)象提供這一實(shí)例。
單例模式確保某個(gè)類只有一個(gè)實(shí)例,而且自行實(shí)例化并向整個(gè)系統(tǒng)提供這個(gè)實(shí)例。在計(jì)算機(jī)系統(tǒng)中,線程池、緩存、日志對(duì)象、對(duì)話框、打印機(jī)、顯卡的驅(qū)動(dòng)程序?qū)ο蟪1辉O(shè)計(jì)成單例。這些應(yīng)用都或多或少具有資源管理器的功能。每臺(tái)計(jì)算機(jī)可以有若干個(gè)打印機(jī),但只能有一個(gè)Printer Spooler,以避免兩個(gè)打印作業(yè)同時(shí)輸出到打印機(jī)中。每臺(tái)計(jì)算機(jī)可以有若干通信端口,系統(tǒng)應(yīng)當(dāng)集中管理這些通信端口,以避免一個(gè)通信端口同時(shí)被兩個(gè)請(qǐng)求同時(shí)調(diào)用??傊?,選擇單例模式就是為了避免不一致狀態(tài),避免政出多頭。
首先看一個(gè)經(jīng)典的單例實(shí)現(xiàn)。
public class Singleton {
private static Singleton uniqueInstance = null;
private Singleton() {
// Exists only to defeat instantiation.
}
public static Singleton getInstance() {
if (uniqueInstance == null) {
uniqueInstance = new Singleton();
}
return uniqueInstance;
}
// Other methods...
}
Singleton通過將構(gòu)造方法限定為private避免了類在外部被實(shí)例化,在同一個(gè)虛擬機(jī)范圍內(nèi),Singleton的唯一實(shí)例只能通過getInstance()方法訪問。(事實(shí)上,通過Java反射機(jī)制是能夠?qū)嵗瘶?gòu)造方法為private的類的,那基本上會(huì)使所有的Java單例實(shí)現(xiàn)失效。此問題在此處不做討論,姑且掩耳盜鈴地認(rèn)為反射機(jī)制不存在。)
但是以上實(shí)現(xiàn)沒有考慮線程安全問題。所謂線程安全是指:如果你的代碼所在的進(jìn)程中有多個(gè)線程在同時(shí)運(yùn)行,而這些線程可能會(huì)同時(shí)運(yùn)行這段代碼。如果每次運(yùn)行結(jié)果和單線程運(yùn)行的結(jié)果是一樣的,而且其他的變量的值也和預(yù)期的是一樣的,就是線程安全的?;蛘哒f:一個(gè)類或者程序所提供的接口對(duì)于線程來說是原子操作或者多個(gè)線程之間的切換不會(huì)導(dǎo)致該接口的執(zhí)行結(jié)果存在二義性,也就是說我們不用考慮同步的問題。顯然以上實(shí)現(xiàn)并不滿足線程安全的要求,在并發(fā)環(huán)境下很可能出現(xiàn)多個(gè)Singleton實(shí)例。
//////////////////////////////////////////////////////////////////////
驗(yàn)證單例模式的示例
//////////////////////////////////////////////////////////////////////
public class TestStream {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// 該類只能有一個(gè)實(shí)例
private TestStream() {
} // 私有無參構(gòu)造方法
// 該類必須自行創(chuàng)建
// 有2種方式
private static TestStream ts1 = null;
// 這個(gè)類必須自動(dòng)向整個(gè)系統(tǒng)提供這個(gè)實(shí)例對(duì)象
public static TestStream getTest() {
if (ts1 == null) {
ts1 = new TestStream();
}
return ts1;
}
public void getInfo() {
System.out.println("output message " + name);
}
public static void main(String[] args) {
TestStream s = TestStream.getTest();
s.setName("張孝祥 1");
System.out.println(s.getName());
TestStream s1 = TestStream.getTest();
s1.setName("張孝祥 2");
System.out.println(s1.getName());
s.getInfo();
s1.getInfo();
if (s == s1) {
System.out.println("創(chuàng)建的是同一個(gè)實(shí)例");
} else if (s != s1) {
System.out.println("創(chuàng)建的不是同一個(gè)實(shí)例");
} else {
System.out.println("application error");
}
}
}
////////////////////////////////////////////
第二個(gè)單例代碼不嚴(yán)謹(jǐn),可能會(huì)創(chuàng)建出多個(gè)實(shí)例。比如有兩個(gè)線程同時(shí)訪問getInstance(),當(dāng)?shù)谝粋€(gè)線程進(jìn)入synchronized塊但還沒有new的時(shí)候,第二個(gè)線程也走到if處,這個(gè)時(shí)候uniqueInstance =null,if返回true,第二個(gè)線程就進(jìn)入了if塊,但在synchronized塊外面等待,然后第一個(gè)線程創(chuàng)建實(shí)例,第二個(gè)線程也能成功創(chuàng)建實(shí)例。
不知道我說得夠不夠清楚。你可以看看《effective Java》這本書,里面有專門講到這個(gè)問題。
單例模式(Singleton) ,屬于最常見的設(shè)計(jì)模式之一,大部分系統(tǒng)都會(huì)用到,目的是為了維護(hù)系統(tǒng)中唯一的一個(gè)實(shí)例。
可分為eager模式,示例代碼如下:
Java代碼
1.class EagerSingleton{
2. private static final EagerSingleton m_instance = new EagerSingleton();
3. private EagerSingleton(){}
4. public static EagerSingleton getInstance(){
5. return m_instance;
6. }
7.}
class EagerSingleton{
private static final EagerSingleton m_instance = new EagerSingleton();
private EagerSingleton(){}
public static EagerSingleton getInstance(){
return m_instance;
}
}
和 lazy模式,示例代碼如下:
Java代碼
1.class LazySingleton{
2. private static LazySingleton m_instance = null;
3. private LazySingleton(){}
4. public synchronized static getInstance(){
5. if(m_instance == null){
6. m_instance = new LazySingleton();
7. }
8. return m_instance;
9. }
10.}
class LazySingleton{
private static LazySingleton m_instance = null;
private LazySingleton(){}
public synchronized static getInstance(){
if(m_instance == null){
m_instance = new LazySingleton();
}
return m_instance;
}
}
java源碼中,Runtime.getRuntime()就是單例的一個(gè)例子。
單例模式的精神就是整個(gè)系統(tǒng)中維護(hù)一個(gè)實(shí)例,推廣開來,如果在一個(gè)系統(tǒng)中需要維護(hù)多個(gè)示例,那么就產(chǎn)生了多例模式(multiton)。
多例模式(Multiton) ,通過聚集對(duì)象了保留自身的多個(gè)示例,根據(jù)客戶端的參數(shù)返回所需要的實(shí)例。
示例代碼如下:
Java代碼
1.class Multiton{
2. private final int INSTANCE_SIZE = 10;
3. private static Map instances = new HashMap(INSTANCE_SIZE);
4. private String name;
5. private Multiton(){}
6. private Multiton(String name){
7. this.name = name;
8. }
9. public synchronized static getInstance(String name){
10. if(instances.containsKey(name)){
11. return instances.get(name);
12. }
13. else{
14. ins = new Multiton(name);
15. instances.put(name, ins);
16. return ins;
17. }
18. }
19.}
class Multiton{
private final int INSTANCE_SIZE = 10;
private static Map instances = new HashMap(INSTANCE_SIZE);
private String name;
private Multiton(){}
private Multiton(String name){
this.name = name;
}
public synchronized static getInstance(String name){
if(instances.containsKey(name)){
return instances.get(name);
}
else{
ins = new Multiton(name);
instances.put(name, ins);
return ins;
}
}
}
[nextpage]
一個(gè)實(shí)用的例子就是KeyGenerator, 示例代碼如下:
Java代碼
1.class KeyGenerator{
2. private final int POOL_SIZE = 20;
3. private static Map instances = new HashMap(16);
4. private KeyInfo keyinfo;
5. private KeyGenerator(){}
6. private KeyGenerator(String keyName){
7. this.keyinfo = new KeyInfo(POOL_SIZE, keyName);
8. }
9. public synchronized static getInstance(String keyName){
10. if(instances.containsKey(keyName)){
11. return (KeyGenerator)instances.get(keyName);
12. }
13. else{
14. keyGen = new KeyGenerator(keyName);
15. instances.put(name, keyGen);
16. return keyGen;
17. }
18. }
19. public synzhronized int getNextKey(){
20. return keyinfo.getNextKey();
21. }
22. }
class KeyGenerator{
private final int POOL_SIZE = 20;
private static Map instances = new HashMap(16);
private KeyInfo keyinfo;
private KeyGenerator(){}
private KeyGenerator(String keyName){
this.keyinfo = new KeyInfo(POOL_SIZE, keyName);
}
public synchronized static getInstance(String keyName){
if(instances.containsKey(keyName)){
return (KeyGenerator)instances.get(keyName);
}
else{
keyGen = new KeyGenerator(keyName);
instances.put(name, keyGen);
return keyGen;
}
}
public synzhronized int getNextKey(){
return keyinfo.getNextKey();
}
}