Manikandan Kbk DIP
Manikandan Kbk DIP

Reputation: 483

How to make sure two thread access same resource and make them Thread safe?

I'm new to multithreading and I have some basic question for which I could not get answers for. Let's say I have a class named BankTransaction and I want to handle multithreading case in there.

CASE 1 (Application based singleton)

@Singleton (Application based singleton)
class BankTransaction {
    synchronized void doSomeTransaction() {
        // write operation
    }
}

Case 2 (Request based singleton)

@Singleton (Request based singleton)
class BankTransaction{
    synchronized void doSomeTransaction() {
        // write operation
    }
}

Questions

Please bear with me and explain me in layman terms as I need to get things cleared.

  1. In Case 1, if 1000 users are making use of the same instance (as it is application based singleton) of class BankTransaction and use the method doSomeTransaction(), the requests for each of the 1000 users be slow? because it is synchronized and will work Thread-safe. So, it is bad approach right. Is my understanding correct?

  2. In Case 2, if the same 1000 users are making use of doSomeTransaction() method of class BankTransaction, each will get their own instances of class BankTransaction and it will be never slow for any one of them. But how do I handle race condition when say for example out of those 1000 users, 2 of them are closely related (husband and wife) and are accessing same bank account and doing doSomeOperation() method at the same time. How do I handle this? because both of them again have 2 different instances of class BankTransaction and data inconsistency would occur.

Upvotes: 0

Views: 1855

Answers (2)

Manish Bansal
Manish Bansal

Reputation: 2681

  1. Yes. Your understanding is correct. You should make a method synchronized if somehow you don't want to allow multiple users to change the state of an object. The example could be application level configuration object.

  2. Now, there are many solutions for this. Easiest could be to defer this problem to some ORM solution which can further use record level lock technique or anything. However, if you want to handle this manually, easiest way to do this is to maintain a column in db. Now, every time you modify the record, you should increment that value. And before committing your record, you should check if the value is same as that of the record you fetched from db. If its same, then you can be sure that no one has modified it since you started your transaction. However, if the value is not same, you can thow an error to the user saying record has been modified by other user.

Upvotes: 1

Tobias
Tobias

Reputation: 2575

  1. In the first Case the 1000 users (or their threads) would wait a long time because only one can get access to the method at a time. All others will have to wait for the lock to get the access to this method. So, yes, it would be slow.

  2. If every user has his own instance of the class BankTransaction it will NOT be slow, because the lock on the method is for an object (the lock object would be "this" in this implementation). So the users (or their threads) only wait if there is another user that wants to use the same method of exactly the same object (what should not be the case if every user has his own instance).

Upvotes: 1

Related Questions