2 回答

TA貢獻(xiàn)1825條經(jīng)驗 獲得超4個贊
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | class Singleton { private static Singleton s; public String str = "你好";
public String getStr(String tName) throws Exception { synchronized (Singleton.class) { //獲取資源,保證多線程下不會導(dǎo)致線程沖突 System.out.println(tName + "進(jìn)入"); Thread.sleep(1000); System.out.println(tName + "結(jié)束"); } return str; }
private Singleton() { System.out.println("A Singleton Model example"); }
public static Singleton getSigleton() { if (s == null) s = new Singleton(); return s; } } |
- 2 回答
- 0 關(guān)注
- 859 瀏覽
添加回答
舉報