請看下面的類,告訴我下面的代碼是否是線程安全的。我的問題的關鍵是,一個類的static
方法和該方法調用單例實例的方法。另外,static
方法由Runnable
實例調用。所以我問你們看代碼 - static
方法,它在多線程環境中調用單例方法 - 是否安全?單身和多線程
如果你回答我的問題,我將非常感激。
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
public class SingletonCls {
private static SingletonCls singletonInstance = null;
private SingletonCls() {
}
public static SingletonCls getIntance() {
if (SingletonCls.singletonInstance == null) {
singletonInstance = new SingletonCls();
}
return SingletonCls.singletonInstance;
}
public List<Map<String, String>> call(String id) throws Exception {
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
BufferedReader br = null;
final String col = "col";
try {
br = new BufferedReader(new FileReader("test.txt"));
String lineStr = null;
while ((lineStr = br.readLine()) != null) {
StringTokenizer st = new StringTokenizer(lineStr, ",");
int colIdx = 1;
if (lineStr.startsWith(id)) {
Map<String, String> map = new HashMap<String, String>();
while (st.hasMoreTokens()) {
String value = st.nextToken();
map.put(col + (colIdx++), value);
}
list.add(map);
}
}
} finally {
if (br != null) {
br.close();
}
}
return list;
}
}
import java.io.IOException;
import java.util.List;
import java.util.Map;
public class TestSingleTonCaller {
public static List<Map<String, String>> getData(String id) throws Exception {
List<Map<String, String>> list = SingletonCls.getIntance().call(id);
return list;
}
}
import java.io.IOException;
import java.util.List;
import java.util.Map;
public class RunnableSingleTonExe implements Runnable {
private final String id;
public RunnableSingleTonExe(String inId) {
this.id = inId;
}
public void run() {
try {
List<Map<String, String>> list = TestSingleTonCaller
.getData(this.id);
System.out.println("thread id:" + this.id + " list > "
+ (list == null ? "" : list.toString()));
} catch (IOException e) {
Thread.currentThread().interrupt();
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
由於您沒有正確創建單例,因此沒有。不能保證你只會有一個'SingletonCls'實例。在Java中對單例使用'enum' –
'SingletonCls#getInstance'方法不是線程安全的。最好從頭開始初始化'singletonInstance',而不是延遲加載它。 –
@Yonyou Ryu - As Brain和Luiggi表示,這不是線程安全的,有更好的方法。請參閱http://stackoverflow.com/questions/3635396/pattern-for-lazy-thread-safe-singleton-instantiation-in-java其他方式來做到這一點。 – lreeder