This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Darya Akbari
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Henry Wong
author
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Darya Akbari
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1855
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
: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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic