File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes A question of lock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "A question of lock" Watch "A question of lock" New topic
Author

A question of lock

Paul Sun
Greenhorn

Joined: Aug 26, 2002
Posts: 19
Hi:
I meet a problem of lock. For example, A,B and C
want to book the same ticket at the same time. Then A will book it and B,C are locked. But when the lock is released by A, lockManager will notify all members, B and C. So that, B and C will try to book it at same time. I think, I need to create a FIFO array to store waitting members and let them book ticket in order. Please give me some advice. Thanks.
Paul
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Paul,
That is what wait, and notifyAll in the Object class is for. When you get an object lock,there is a battle for the lock when three client go for it.
When A releases the lock it calls notifyAll() and lets B and C fight for the lock. That is the best way to handle it, because it is all handled by the JVM. That is threads at it's greatest.
So it is better to use what is already provided for you than trying to reinvent the wheel. I remember when I was a kid programmer I kept trying to reinvent the wheel. You should see how many triangular wheels I have, the most bizarre one is Red and has Big letters on it saying STOP.
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451

Originally posted by Mark Spritzler:
... I remember when I was a kid programmer I kept trying to reinvent the wheel. You should see how many triangular wheels I have, the most bizarre one is Red and has Big letters on it saying STOP.

I wish I could remember when I was a kid programmer! I vaguely recall terms like punch-cards, EBCDIC, and magnetic core. I even remember praying for 16K of core (RAM) instead of the 8K we had to work with.
Michael Morris
OFP
(Old Fart Programmer)


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Well I programmed on a Vic 20 when it had only 4K of memory. and before that was the Commodore Pet and the Trash 80's by Radio Shack.
Mark
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Mark

Well I programmed on a Vic 20 when it had only 4K of memory. and before that was the Commodore Pet and the Trash 80's by Radio Shack.

Those guys are state of the art compared to my firsts: the IBM 1130 whose console was a teletype and whose printer was nearly as big as Volkswagen and slower than a one-legged dog; and the IBM 360 "mainframe".
You did remind me of the last assembly routine I wrote. It was on the old Trash 80, an interrupt routine for a (now defucnt) Houston Instruments plotter.
Michael Morris
 
GeeCON Prague 2014
 
subject: A question of lock