Tutorial Addendum On Java - Synchronization

financeappsios wordgamesios arcadegamesios
 31 December 18:00   

    



    



    

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<a_maxi; j++) {

    

System.out.print("
"+j);

    

continued sum = 0;

    

for (int i=0; i<t_maxi; i++) {

    

sum += t_list.getBalance(j);

    

System.out.print(", "+getCurrency(t_list.getBalance(j)));

    

}

    

System.out.print(", "+getCurrency(sum)+

    

", "+getCurrency(balance));

    

}

    

System.out.print("
# of Transactions");

    

for (int i=0; i<t_maxi; i++)

    

System.out.print(", "+t_list.transactionCount());

    

System.out.print("
");

    

}

    

clandestine changeless Cord getCurrency(long d) {

    

continued i = d/100;

    

continued f = Math.abs(d)%100;

    

Cord fs = String.valueOf(f);

    

if (f<10) fs = "0"+fs;

    

acknowledgment String.valueOf(i)+"."+fs;

    

}

    

accessible changeless continued getStep() {

    

acknowledgment t_step;

    

}

    

accessible changeless int accountCount() {

    

acknowledgment a_maxi;

    

}

    

accessible changeless Accidental getRandom() {

    

acknowledgment r_number;

    

}

    

accessible changeless continued getBalance(int i) {

    

acknowledgment balance;

    

}

    

accessible changeless boolean doTransaction1(int i, continued a) {

    

boolean ok = false;

    

continued c = balance; // get accepted balance

    

ok = c+a>0;

    

if (ok) { // stop accout antithesis traveling negative

    

try {

    

Thread.sleep(1); // apathetic down the process

    

} bolt (InterruptedException e) {

    

Thread.currentThread().interrupt(); // re-set the flag

    

}

    

balance = c+a; // set accepted balance

    

}

    

acknowledgment ok;

    

}

    

accessible synchronized changeless boolean doTransaction2(int i, continued a) {

    

boolean ok = false;

    

continued c = balance; // get accepted balance

    

ok = c+a>0;

    

if (ok) { // stop accout antithesis traveling negative

    

try {

    

Thread.sleep(1); // apathetic down the process

    

} bolt (InterruptedException e) {

    

Thread.currentThread().interrupt(); // re-set the flag

    

}

    

balance = c+a; // set accepted balance

    

}

    

acknowledgment ok;

    

}

    

accessible changeless boolean doTransaction3(int i, continued a) {

    

boolean ok = false;

    

synchronized (o_lock) {

    

continued c = balance; // get accepted balance

    

ok = c+a>0;

    

if (ok) { // stop accout antithesis traveling negative

    

try {

    

Thread.sleep(1); // apathetic down the process

    

} bolt (InterruptedException e) {

    

Thread.currentThread().interrupt(); // re-set the flag

    

}

    

balance = c+a; // set accepted balance

    

}

    

}

    

acknowledgment ok;

    

}

    

accessible changeless boolean doTransaction4(int i, continued a) {

    

boolean ok = false;

    

synchronized (a_lock) {

    

continued c = balance; // get accepted balance

    

ok = c+a>0;

    

if (ok) { // stop accout antithesis traveling negative

    

try {

    

Thread.sleep(1); // apathetic down the process

    

} bolt (InterruptedException e) {

    

Thread.currentThread().interrupt(); // re-set the flag

    

}

    

balance = c+a; // set accepted balance

    

}

    

}

    

acknowledgment ok;

    

}

    

}

    



    



    

Couple of addendum about this program:

    



        

  • BankingThread aswell keeps clue of antithesis of anniversary annual with t_balance.

        

    The sum of t_balance from every cilia should according to the antithesis in the

        

    BankingMain class, balance.

        



  •     

  • BankingTread.run() simulates a transaction from a chump by about

        

    generating an annual number, a transaction type, and a transaction amount.

        



  •     

  • I use "long" to abundance and account bill accompanying values, because it

        

    is added authentic than "double".

        



  •     

  • The analysis accomplish are acclimated to ascendancy the tests of altered synchronization

        

    implementations: no synchronization, synchronized method, synchronized

        

    statements, and synchronized statements per account.

        



  •     

  • The Thread.sleep(1) account in doTransaction() methods is acclimated to increase

        

    the beheading time of the transaction. If the cilia is signaled for interruption

        

    during this sleep, no activity is taken, but re-set the abeyance banderole to let

        

    BankingThread chic to handle it later.

        



  •     

  • All instances of BankingThread are created withing a ThreadGroup, so I can

        

    easily forward an abeyance arresting to all of them.

        



  •     

  • The "while (g.activeCount()>0);" account in the BankingMain chic is

        

    used to create abiding all treads are concluded afore accession after-effects from them.

        



  •     



    



    



    

 


Tags: account, transaction, balance, system, class, process, sleep, catch, notes, public, string, false, print

 balance, thread, static, print, system, return, public, current, boolean, synchronized, sleep, transaction, process, catch, currentthread, interrupt, getcurrency, interruptedexception, synchronization, string, negative, false, accout, account, bankingthread, class, notes, getbalance, , system out, current balance, public static, thread sleep, slow down, thread currentthread, currentthread interrupt, catch interruptedexception, balance going, static boolean, stop accout, accout balance, negative try, balance going negative, thread currentthread interrupt, accout balance going, stop accout balance, public static boolean, public static long,

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

Permalink
Article In : Computers & Technology  -  java