简体   繁体   English

多线程和共享对象

[英]Multi threading and shared object

If I have a class like : 如果我有一个像这样的课程:

class MultiThreadEg {

private Member member;

public Integer aMethod() {
    ..............
    ..............
}

public String aThread() {
    ...............
    member.memberMethod(.....);
    Payment py = member.payment();
    py.processPayment();
    ...........................
}

}

Suppose that aThread() is a new thread, then, will accessing the shared member object by too many threads at the same time cause any issues (with the following access rules)? 假设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.

for eg: In the payment() method, I do this : 例如:在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;
}

}

My concern is that, even though I create object copies for "writing" (like in the method payment() ), acessing the member object by too many threads at the same time will cause some discrepancies. 我担心的是,即使我创建了用于“写入”的对象副本(例如在payment()方法中),但同时通过太多线程访问成员对象仍会引起一些差异。

What is the fact ? 事实是什么? Is this implementation reliable in every case (0 or more concurrent accesses) ? 此实现在每种情况下(0个或多个并发访问)是否可靠? Please advise. 请指教。 Thanks. 谢谢。

You could simply use a ReentrantReadWriteLock . 您可以简单地使用ReentrantReadWriteLock That way, you could have multiple threads reading at the same time, without issue, but only one would be allowed to modify data. 这样,您可以让多个线程同时读取,而不会出现问题,但是只允许一个线程修改数据。 And Java handles the concurrency for you. Java为您处理并发。

 ReadWriteLock rwl = new ReentrantReadWriteLock();
 Lock readLock = rwl.readLock;
 Lock writeLock = rwl.writeLock;

 public void read() {

    rwl.readLock.lock();
    try {
       // Read as much as you want.
    } finally {
       rwl.readlock.unlock();
    }
 }

 public void writeSomething() {
    rwl.writeLock.lock();
    try {
       // Modify anything you want
    } finally {
       rwl.writeLock.unlock();
    }
 }

Notice that you should lock() before the try block begins, to guarantee the lock has been obtained before even starting. 请注意,您应该在try块开始之前将lock()锁定 ,以确保甚至在启动之前就已经获得了锁定。 And, putting the unlock() in the finally clause guarantees that, no matter what happens within the try (early return, an exception is thrown, etc), the lock will be released. 并且,将unlock()放在finally子句中可以确保,无论try内发生了什么(提早返回,抛出异常等),该锁都会被释放。

In case update to memPay depends on the memPay contents (like memPay.amount+=100) you should block access for other threads when you are updating. 如果要更新memPay取决于memPay内容(例如memPay.amount + = 100),则在更新时应阻止对其他线程的访问。 This looks like: 看起来像:

mutual exclusion block start
get copy
update copy
publish copy
mutual exclusion block end

Otherwise there could be lost updates when two threads simultaneously begin update memPay object. 否则,当两个线程同时开始更新memPay对象时,更新可能会丢失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM