无状态类在并发环境中绝对安全吗?
无状态类是指一个对象没有任何属性,就像下面这个类一样,通常来说无状态的类在并发环境中比较安全:
?
class class1 { public void dosomething() {vector<string> vector = new vector<string>();string element = "";if (!vector.contains(element)) { vector.add(element);} } }?public class class2 { private static vector<string> vector = new vector<string>(); public vector<string> getvector() {return vector; } public static void main(string[] args) {class3 c = new class3();thread t = new thread(new runnable() { public void run() {class3 c = new class3();c.dosomething(); }});t.start();c.dosomething();system.out.println(vector); }}??class class3 { public void dosomething() {vector<string> vector;class2 c = new class2();vector = c.getvector();string element = "cantellow";if (!vector.contains(element)) {//在此处打上断点 vector.add(element);} }}?用eclipse在<span style="white-space: pre;">if (!vector.contains(element))打上断点,</span>