This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Synchronisation in java  RSS feed

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was asked a use case in one of the interviews where the interviewer asked:

Interviewer : Suppose you have a bank account and you are doing online shopping
Your brother has your ATM card and he is about to make a transaction
Your father has gone to give a withdrawal cheque in the bank.
All these 3 transactions happen at the same time
How will these transactions be managed such that the balance is not overdrawn?

Me: I said I would use synchronisation on the account object

Interviewer : Not satisfied. Next question.

Can someone please explain what could have been the answer? Would a database lock or transaction isolation be a better approach?
 
Ranch Hand
Posts: 70
1
Firefox Browser Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I can say there is a preference to online transaction processing compared namely, to ATM and Physical transaction at offices.

Answer: ATM can be reflected according to online with automatic balancing at every seconds. During physical transactions, it continuously applies too.

Regarding lock and synchronization: it's for concurrent or parallel processing.

Physical is not a parallel, or even ATM/Online could be parallel.

Thanks.


 
saurabhmehta mehta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Noorul Hameed wrote:Hi,


Regarding lock and synchronization: it's for concurrent or parallel processing.




What if both the online and ATM transaction occur at the exact same time? Shouldn't there be some locking at the database level?
 
Noorul Hameed
Ranch Hand
Posts: 70
1
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If this belongs to database means:
Lock or synchonized processes

Or, if online means, thread priority is being assigned.

Top level priority yields results at first.

Maybe intervier expected concepts of locking and its priorities.

Is it  okay?

Thanks.
 
Marshal
Posts: 61718
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mistake No 1: If an interviewer asks about synchronisation, don't go thinking he wants an answer about synchronisation.
He wants evidence that you have thought about that sort of question.  You should show that you know there are several ways to settle that problem, and that you know whether you are working in serial, concurrently or in parallel. Actually I don't think there is a difference to the solution between concurrency and parallelism. You should mention several possibilities and then say which of them you think is the most important. I would have made the statements at the database into transactions.

Moving to our Jobs forum.
 
saurabhmehta mehta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Mistake No 1: If an interviewer asks about synchronisation, don't go thinking he wants an answer about synchronisation.
He wants evidence that you have thought about that sort of question.  You should show that you know there are several ways to settle that problem, and that you know whether you are working in serial, concurrently or in parallel. Actually I don't think there is a difference to the solution between concurrency and parallelism. You should mention several possibilities and then say which of them you think is the most important. I would have made the statements at the database into transactions.

Moving to our Jobs forum.



So if we go by the database level then transaction isolation level like serializable, reapeatable read etc comes into play right?
 
saurabhmehta mehta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Noorul Hameed wrote:Hi,

If this belongs to database means:
Lock or synchonized processes

Or, if online means, thread priority is being assigned.

Top level priority yields results at first.

Maybe intervier expected concepts of locking and its priorities.

Is it  okay?

Thanks.




So if we go by the database level then transaction isolation level like serializable, repeatable read etc comes into play right?

 
Noorul Hameed
Ranch Hand
Posts: 70
1
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

saurabhmehta mehta wrote:

Campbell Ritchie wrote:Mistake No 1: If an interviewer asks about synchronisation, don't go thinking he wants an answer about synchronisation.
He wants evidence that you have thought about that sort of question.  You should show that you know there are several ways to settle that problem, and that you know whether you are working in serial, concurrently or in parallel. Actually I don't think there is a difference to the solution between concurrency and parallelism. You should mention several possibilities and then say which of them you think is the most important. I would have made the statements at the database into transactions.

Moving to our Jobs forum.



So if we go by the database level then transaction isolation level like serializable, reapeatable read etc comes into play right?



Hi,
Database lock can be answer. Because  one transaction is allowed and  next queued.
 
Campbell Ritchie
Marshal
Posts: 61718
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't quote the whole of a previous post. That simply makes the thread longer without adding new information.

Yes, you should mention serializable and repeatable read. Would you lock the database? Would locking prevent parallel statements affecting other customers' records? The interviewer wants to know whether you have ever thought of that sort of consideration; that is more important than knowing the details. If you aren't sure of the details, you can always name a book with such details in.
 
Bartender
Posts: 19976
95
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Noorul Hameed wrote:Physical is not a parallel, or even ATM/Online could be parallel.



I would be very interested to hear that statement translated into English.

If you've got 3 people attempting to alter the same account "in parallel", I'd consider those to be concurrent operations, to use the technical term for it. What you want to avoid is simultaneous operations, where the operations are not merely concurrent (parallel), but acting independently.

And yes, you need some sort of synchronization interlock to maintain data integrity while the concurrent operations are going on.

The question is false. The danger is not that 3 simultaneous requests would overdraw the account, but that the account balance would be corrupted.

Allow me to demonstrate. To keep things clearer, I'll use only 2 participants, rather than 3.

Let's say that we have two gentlemen, Mr. Singh and Mr. Chandraputra who share the same account. Mr. Singh goes to the bank and withdraws 50000 rupees. Mr. Chandraputra concurrently goes to an ATM and withdraws 3000 rupees.

The shared computer system shows a balance of at least Rs 50000, so the teller hands Mr. Singh the cash and debits the acccount online. However, while the account transaction is in process, the ATM transaction kicks in. Both transactions pull the same starting balance, but because they are not merely concurrent, but are simultaneous, the updating will occur without taking the other person's transaction into account. So at the end of both transactions, instead of showing a net debit of Rs 53000, the account could potentially only show the debit of Rs 3000 and the bank has just lost Rs 50000.

Therefore you do need a synchronisation mechanism. What kind of mechanism depends on your IT infrastructure. If there's a master Java application handling all withdrawals, regardless of source, you could synchronise on the javabean representing the account (if there is one), most definitely.

More commonly, though, you'd lock the account data at the database level, do the transaction, then commit the change, Or if the account has overdraft protection and the withdrawal would exceed the balance, reject the change and rollback the transaction.

On a historical note, in the real world, banks actually didn't update balances in real time. Instead, they posted transactions that were batched up and applied overnight. Even ATM transactions weren't posted in real time. These days, probably everyone does actually update in real time, as traditional mainframe-style batch processing is out of fashion.
 
Campbell Ritchie
Marshal
Posts: 61718
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:. . . in the real world, banks actually didn't update balances in real time. Instead, they posted transactions that were batched up and applied overnight. . . .

They had a paper bank statement sheet for each customer, which was taken out of the file and put into an adding machine, and all transactions were printed on it by the adding machine; negative figures caused it to use red ink. The sheets were duplicated and once a month one copy was posted to the customer as a statement. That is what my bank statements looked like for the first year I had the account; when I moved to a different branch, they used a basic computerised system.
 
Campbell Ritchie
Marshal
Posts: 61718
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A few minutes ago, I wrote:. . . red ink. . . .

That is, of course, the origin of the phrase “in the red”. I think the red ink was in a two‑coloured typewriter ribbon.
 
Noorul Hameed
Ranch Hand
Posts: 70
1
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Deadlock is another solution that I refer here.

I just read the sections on chapter 10 in "Java Concurrency in Practice."

Author used the deadlock concepts in pp.130, as follows.

Oneway to induce an ordering on objects is to use System.identityHashCode,which returns the value that would be returned by Object.hashCode.

See code section as follows.
    int fromHash = System.identityHashCode(fromAcct);     int toHash = System.identityHashCode(toAcct);    
 if (fromHash < toHash) {        
synchronized (fromAcct) {            
synchronized (toAcct) {                
new Helper().transfer();            
}         }  
  } else if (fromHash > toHash) {        
synchronized (toAcct) {            
synchronized (fromAcct) {                
new Helper().transfer();             }         }    
} else {        
synchronized (tieLock) {          
 synchronized (fromAcct) {                
synchronized (toAcct) {                    
new Helper().transfer();
               }          
 }      
  }
 
Tim Holloway
Bartender
Posts: 19976
95
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Tim Holloway wrote:. . . in the real world, banks actually didn't update balances in real time. Instead, they posted transactions that were batched up and applied overnight. . . .

They had a paper bank statement sheet for each customer, which was taken out of the file and put into an adding machine, and all transactions were printed on it by the adding machine; negative figures caused it to use red ink. The sheets were duplicated and once a month one copy was posted to the customer as a statement. That is what my bank statements looked like for the first year I had the account; when I moved to a different branch, they used a basic computerised system.



Young whippersnapper! In my day, they wrote it all down on a sheet of paper and gave it to Scrooge & Marley where Bob Cratchitt would write it all down in the ledger books!

Seriously, you're either out-aging me or the fact that at one time my town was a regional banking and insurance capital meant that for a change we weren't vying for the most technologically backwards spot in the nation.

Red/black ink ribbons are a thing. Some typewriters even had a ribbon shift key. Not only banking machines had dual-colour capability, though, some of IBM's general business tabulating machines did as well.
 
Campbell Ritchie
Marshal
Posts: 61718
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:. . . . Some typewriters even had a ribbon shift key.  . . . .

I used to have one of them. It had three positions, •. The white   was used for cutting stencils, without using the ribbon. I gave up buying black and red ribbons because I never seemed to use the red.
 
Tim Holloway
Bartender
Posts: 19976
95
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Tim Holloway wrote:. . . . Some typewriters even had a ribbon shift key.  . . . .

I used to have one of them. It had three positions, •. The white   was used for cutting stencils, without using the ribbon. I gave up buying black and red ribbons because I never seemed to use the red.



Oh yeah. It wasn't a key, it was a little lever.

I think the primary advantage of the red ribbon was in doing homework where you'd print quotations in red.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!