• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem in synchronization while using multiple objects

 
saurabh kuma
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,

this may be a very basic question, but I have tried a lot to sort it myself.

I am trying to synchronize following code segment, which exactly it is a bank example where in transfer money from one account to another. Each bank object has a transfer method, which in turns call debit and credit methods of Account object.

Please suggest and also please tell me where I am doing this wrong.

Thanks a lot in advance


 
Henry Wong
author
Sheriff
Posts: 22541
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saurabh kuma wrote:
Please suggest and also please tell me where I am doing this wrong.

Thanks a lot in advance


Considering that we don't know what you are trying to do, don't know what you are encountering, and don't know what you are expecting, isn't it kinda hard for us to tell you what you are doing wrong? ...

How about giving us a bit more context ?

Henry
 
saurabh kuma
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, that needed to be more descriptive,

I was just trying my hands on Multithreading, for this I created a Bank example.

In this application we have Bank object and Account objects. Bank object contains an array of Account objects. We call transfer method from bank object with three arguments money, source account number and destination account number respectively. The source account number and destination account numbers are indexes of accounts array in Bank object. The transfer object then calls debit and credit method of corresponding account. At the end the total money in bank should be sum of money in all the accounts of bank taken togather and should be equal to sum of initial money which is 15000. This is checked by getBankMoney method of bank.

Hoever in my case, I always lose or get extra money in bank i.e. I am not able to synchronize. I want to know how I should use locks and synchronize keyword to fix this. I am getting confused in how locks should be used when a synchronized method calls other synchronized methods of a different class.

I donot want to use java.concurrent package right now.

Thanks
Saurabh
 
Henry Wong
author
Sheriff
Posts: 22541
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saurabh kuma wrote:
In this application we have Bank object and Account objects. Bank object contains an array of Account objects. We call transfer method from bank object with three arguments money, source account number and destination account number respectively. The source account number and destination account numbers are indexes of accounts array in Bank object. The transfer object then calls debit and credit method of corresponding account. At the end the total money in bank should be sum of money in all the accounts of bank taken togather and should be equal to sum of initial money which is 15000. This is checked by getBankMoney method of bank.

Hoever in my case, I always lose or get extra money in bank i.e. I am not able to synchronize. I want to know how I should use locks and synchronize keyword to fix this. I am getting confused in how locks should be used when a synchronized method calls other synchronized methods.


Hint: Try this... using only a single thread, transfer a million units of money from one account into another.

Henry
 
saurabh kuma
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry, I got the problem and fixed it, a stupid mistake

One more question, I have removed locks from Account methods and it is still working fine. Is there no scenario when Account methods may be run concurrently by different threads?
I have only synchronized Bank, not Account.

This is the modified Correct code




 
Henry Wong
author
Sheriff
Posts: 22541
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saurabh kuma wrote:Thanks Henry, I got the problem and fixed it, a stupid mistake

One more question, I have removed locks from Account methods and it is still working fine. Is there no scenario when Account methods may be run concurrently by different threads?
I have only synchronized Bank, not Account.


Correct, There is a lock at the bank level, so at no time will a transfer (or a sum of accounts) happen concurrently. And since that can't happen, then at no time will an account be accessed concurrently.

BTW, your money transferer classes also have a lock, but since your three money transferer each have different locks, no synchronization is occurring between the money transferers.

Henry
 
saurabh kuma
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understood

Thanks Henry for your time and help
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!