Tutorial Addendum On Java - Synchronization

financeappsios wordgamesios arcadegamesios
 31 December 18:00   




Why Synchronization



The better problem of acceptance assorted accoutrement administration the aforementioned data set


is that one operation in one cilia could bang with addition operation


in addition accoutrement on the aforementioned data. If this happens, the aftereffect is un-desirable.



Let s use a coffer appliance program as an example. Bold that the program


has assorted accoutrement running, with anniversary cilia abutting an ATM system, and


you accept a extenuative annual in the coffer with $100.00, now you and your acquaintance


are traveling to two altered ATMs at about the aforementioned time, and aggravating to abjure


$50.00 from your account, what do you anticipate it will happen?



If the accoutrement are active independently, the afterward could happen:




Time 01:01 02:01 03:01 04:01




Thread 1 Get Set


Action You Annual Annual You


Abjure Antithesis Antithesis Receive


$50.00 $100.00 $50.00 $50.00





Time 01:02 02:02 03:02 04:02




Thread 2 Get Set


Action Acquaintance Annual Annual Friend


Abjure Antithesis Antithesis Receive


$50.00 $100.00 $50.00 $50.00





Time 01:01 02:01 03:01 04:01




Account $100.00 $100.00 $50.00 $50.00




Both you and your acquaintance will accept $50.00 each, and your annual will


still accept $50.00. The coffer could lose $50.00.



The band-aid this problem is synchronization.



What Is Synchronization



Synchronization is a programming address that involves 3 elements:



  • Lock: An item with two states: bound and unlocked.



  • Synchronized Block: A block of statements that is associated


    with a lock.



  • Synchronization Rule: If a synchronized block is encountered in


    a cilia of execution, the associated lock will be checked. If the lock


    locked, the beheading will be chock-full until the lock is unlocked. If the lock


    is unlocked, the lock will be locked, and the synchronized


    block of statements will be executed. If the beheading alcove the end of


    the synchronized block, the lock will be unlocked. With this rule, two synchronized


    blocks associated with aforementioned lock will never be accomplished at the aforementioned time.





Now let s see if we can use the synchronization address in the coffer application


program to advice the bank. Let s ascertain a


synchronization block starting from the "Get Annual Balance" activity to the


"Set Annual Balance" activity in anniversary thread, and accessory the block with


a lock. With this change, both you and your acquaintance can still abjure $50.00,


but your annual will accept annihilation left:




Time 01:01 02:01 03:01 04:02




Lock Apart Bound Bound Unlocked





Time 01:01 02:01 03:01 04:01




Thread 1 Get Set


Action You Annual Annual You


Abjure Antithesis Antithesis Receive


$50.00 $100.00 $50.00 $50.00





Time 01:02 02:02 03:02 04:02 05:02




Thread 2 Get Get Set


Action Acquaintance Annual Annual Annual Friend


Abjure Antithesis Antithesis Antithesis Receive


$50.00 Chock-full $50.00 $0.00 $50.00





Time 01:01 02:01 03:01 04:02 05:02




Account $100.00 $100.00 $50.00 $0.00 $0.00




The synchronization address did advice the coffer from accident money.


But it aswell added the absolute transaction time.




Tags: account, action, balance, block, friend, technique

 account, balance, thread, synchronization, friend, unlocked, withdraw, block, locked, receive, threads, setaction, associated, execution, synchronized, technique, , account account, balance balance, lock will, balance receive, withdraw balance, synchronized block, withdraw balance balance, balance balance receive, friend withdraw balance, account balance action, account friend withdraw, friend account account, setaction friend account, account account friend,

Share Tutorial Addendum On Java - Synchronization:
Digg it!   Google Bookmarks   Del.icio.us   Yahoo! MyWeb   Furl  Binklist   Reddit!   Stumble Upon   Technorati   Windows Live   Bookmark

Text link code :
Hyper link code:

Also see ...

Article In : Computers & Technology  -  java