See more articles about "java "

Tutorial Addendum On Java - Synchronization



 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.

    



    



 


 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 ...

Tutorial Addendum On Java - Synchronization
Synchronization Abutment in JavaInstead of let the programmers to architecture their own locks, administer the synchronization blocks, and administer the synchronization rules, Java

Tutorial Addendum On Java - Synchronization
Synchronization Sample ProgramNow, let s address a Java program to see how the synchronization techniquecan break the coffer problem. Two classes are advised to simulate a bankp

Tutorial Addendum On Java - Synchronization
clandestine changeless abandoned printResult() { System.out.print("Account"); for (int i=0; i<t_maxi; i++) System.out.print(", ATM "+i); System.out.print(", Transaction Sum, Balance"); for (int j=0; j<

Tutorial Addendum On Java - Synchronization
Output: No synchronization:Account, ATM 0, ATM 1, ATM 2, ATM 3, ATM 4, Transaction Sum, Balance0, 506.81, 227.06, 286.89, 885.14, 330.11, 465.73, 1051.871, 63.98, 27.74, 413.05, 276.93, 86.81,

Tutorial Addendum On Java - Accoutrement
Output: Hello world! From the capital program.Hello world! From a thread.Note that:The program behaves the aforementioned way as the antecedent p

Tutorial Addendum On Java - Accoutrement
Output Priority: (min, norm, max) = (1, 5, 10)Threads: 0 1 2 3 4 Time Steps: 1527 2229 161931 31864 29386 17416 Steps: 3193 3881 175188 44342 41757 20301 Steps: 6499 5536 204244 74234

Tutorial Addendum On Java - Accoutrement
The afterward program shows that how assorted accoutrement can plan calm to account how some weekdays in anniversary year for 2000 years starting from year 2000. In this program, an array, y_days, is declared as static,s

Tutorial Addendum On Java - Accoutrement
Output of 5 threads: Thread: 0 1 2 3 4 Year: 0 0 0 0 0 Year: 2039 2040 2043 2042 2044 Year: 2067 2040 2048 2042 2044 Year: 2124 2142 2048 2159 2044 Year: 2318 2180 2230 2300 2

Tutorial Addendum On Java - Accoutrement
/** * TimerThread.java * Absorb (c) 2002 by Dr. Yang */import java.util.*;import java.text.*;class TimerThread extends Cilia { clandestine changeless final int NORMAL_CLOCK = 1; clan

Tutorial Addendum On Java - Cilia Groups
ThreadGroup ClassThreadGroup: A chic represents a accumulating of Cilia altar and ThreadGroup objects. Features of ThreadGroup class: