File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Java Threads 2nd edition: ATM locking example does not synchronize. WHY? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Java Threads 2nd edition: ATM locking example does not synchronize. WHY?" Watch "Java Threads 2nd edition: ATM locking example does not synchronize. WHY?" New topic
Author

Java Threads 2nd edition: ATM locking example does not synchronize. WHY?

Darya Akbari
Ranch Hand

Joined: Aug 21, 2004
Posts: 1855
Dear all,

have anyone of you tried to run the ATM example from Henry Wong's Java Threads 2nd edition? You can find the example on page 62. It uses class BusyFlag to create a session scope lock (locking the session between login and logoff to the ATM)

I tried to run this example and put myself (Darya) and my wife (Mehrak) in two concurrent threads that try to deduct 10$ from our common account with an initial amount of 100$.

Unfortunately my wife is still able to login even when I am in the middle of my procedure. Look to the result



For those who like to tryout on their own machine I put in my classes used for testing:

AutomatedTellerMachine.java


Account.java


BusyFlag.java


TestJob.java


Can someone exlpain why my wife (Mehrak) is still able to login even when I have the lock for the account

Regards,
Darya
[ March 25, 2005: Message edited by: Darya Akbari ]

SCJP, SCJD, SCWCD, SCBCD
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18113
    
  39

Originally posted by Darya Akbari:

Can someone exlpain why my wife (Mehrak) is still able to login even when I have the lock for the account


Your wife can't log in. The method throws the exception that you had coded. If you are referring to why the login method even runs, the lock (busyflag) on the account is in the account object, just synchronizing the method isn't enough.

BTW, I would *really* recommend that you get/borrow the 3rd edition instead. The second edition was printed in 1999, and only covers up to JDK 1.2. If you are preparing for certification, you might want something a bit more up to date.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Darya Akbari
Ranch Hand

Joined: Aug 21, 2004
Posts: 1855
Hi Henry,

thanks for your response.

Originally posted by Henry Wong:

If you are referring to why the login method even runs, the lock (busyflag) on the account is in the account object, just synchronizing the method isn't enough.


You are right, but what else do I need to prohibit my wife to enter even the login

I was thinking for the 3rd edition of Java Threads but it seems that it uses JDK 1.5 new lock mechanism which I don't want to use for the SCJD certificate since I want to understand first the whole locking issue and this by using only JDK 1.4 :roll: .

Regards,
Darya
[ March 25, 2005: Message edited by: Darya Akbari ]
Darya Akbari
Ranch Hand

Joined: Aug 21, 2004
Posts: 1855
:roll: I think I got it. I changed the TestJob class so that the session scope between login and logoff becomes an atomic operation.

Further I had to change the AutomatedTellerMachine class to let me and my wife act on the same account.

The result is now (what I was expecting ):


The changed classes now look as follows:

TestJob.java


AutomatedTellerMachine.java


Regards,
Darya
[ March 25, 2005: Message edited by: Darya Akbari ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Java Threads 2nd edition: ATM locking example does not synchronize. WHY?
 
Similar Threads
Problem with threads (concurrency)
Banking with Threads
Thread question
join() vs. synchronized
Need help with username and password validation