如果我有这样的课程:
class MultiThreadEg {
private Member member;
public Integer aMethod() {
..............
..............
}
public String aThread() {
...............
member.memberMethod(.....);
Payment py = member.payment();
py.processPayment();
...........................
}
}
假设aThread()是一个新线程,那么,太多线程同时访问共享成员对象会不会出现问题(访问规则如下)?
Rule 1 : ONLY reading, no writing to the object(member).
Rule 2 : For all the objects that need some manipulation(writing/modification), a copy of the original object will be created.
例如:在payment()方法中,我这样做:
public class Member {
private Payment memPay;
public payment() {
Payment py = new Payment(this.memPay);//Class's Object copy constructor will be called.
return py;
}
}
我担心的是,即使我为“写入”创建对象副本(例如在方法payment()中),同时通过太多线程访问成员对象也会导致一些差异。
事实是什么?这种实现在每种情况下都可靠吗(0 个或多个并发访问)?请指教。谢谢。