Tutorial Addendum On Java - Synchronization

financeappsios wordgamesios arcadegamesios
 31 December 18:00   




Synchronization Sample Program



Now, let s address a program ming/java/general-java-list/' onMouseOver="tip('infobox1')" onMouseOut="untip()"> Java program to see how the synchronization technique


can break the coffer problem. Two classes are advised to simulate a bank





  • BankingThread class: Acting as an ATM machine, demography drop or abjure


    transaction requests from customers, and allurement the BankingMain chic to


    perform the transactions.



  • BankingMain class: Acting as the ascendancy center, managing instances of


    the BankingThread class, and assuming affairs advancing from anniversary BankingThread







BankingThread class:






* BankingThread.java


* Absorb (c) 2002 by Dr. Yang




import java.util.*;


public chic BankingThread extends Cilia {


clandestine long t_balance; // acount balance


clandestine continued t_count; // amount of affairs done so far


accessible BankingThread(ThreadGroup g, Cord n) {




t_count = 0;


int m = BankingMain.accountCount();


t_balance = new long;


for (int i=0; i<m; i++) t_balance = 0;




accessible continued transactionCount() {


acknowledgment t_count;




accessible continued getBalance(int i) {


acknowledgment t_balance;




accessible abandoned run() {


while (!isInterrupted()) {




Accidental r = BankingMain.getRandom();


continued t_step = BankingMain.getStep();


int m = BankingMain.accountCount();


int n = r.nextInt(m); // annual number


int t = 2*r.nextInt(2)-1; // blazon of transaction


continued a = (long) t*r.nextInt(10000); // bulk of transaction


if (t_step==1)


if (BankingMain.doTransaction1(n,a)) t_balance += a;


if (t_step==2)


if (BankingMain.doTransaction2(n,a)) t_balance += a;


if (t_step==3)


if (BankingMain.doTransaction3(n,a)) t_balance += a;


if (t_step==4)


if (BankingMain.doTransaction4(n,a)) t_balance += a;


try {


sleep(10); // delay for the next customer


} bolt (InterruptedException e) {














BankingMain class:






* BankingMain.java


* Absorb (c) 2002 by Dr. Yang




import java.util.*;


public chic BankingMain {


accessible changeless final int a_maxi = 10; // amount of accounts


accessible changeless long antithesis = new long;


accessible changeless Accidental r_number;


clandestine changeless final int t_maxi = 5; // amount of threads


clandestine changeless BankingThread t_list = new BankingThread;


clandestine changeless continued t_step;


clandestine changeless Item o_lock;


clandestine changeless Object a_lock = new Object;


accessible changeless abandoned main(String a) {


o_lock = new Object();


for (int i=0; i<a_maxi; i++) a_lock = new Object();


t_step = 1; // using doTransaction1();


System.out.println("No synchronization:");






t_step = 2; // using doTransaction2();


System.out.println("Synchronized method:");






t_step = 3; // using doTransaction3();


System.out.println("Synchronized statements:");






t_step = 4; // using doTransaction4();


System.out.println("Synchronized statements per account:");








clandestine changeless abandoned testATM() {


for (int i=0; i<a_maxi; i++) balance = 0;


r_number = new Random();


ThreadGroup g = new ThreadGroup("ATM");


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


BankingThread t = new BankingThread(g,String.valueOf(i));




t_list = t;




try {


Thread.sleep(5*1000); // alive period


} bolt (InterruptedException e) {


System.out.println("Interrupted in the mainthread.");






while (g.activeCount()>0); // delay for all accoutrement to end








Tags: transaction, balance, system, class, statements, public, string, transactions, object

 bankingmain, balance, static, bankingthread, public, private, class, println, system, testatm, synchronization, object, count, printresult, synchronized, threadgroup, transaction, transactions, string, random, nextint, , private static, system out, testatm printresult, public static, println synchronized, new object, bankingthread class, println synchronized statements, private static object, maxi public static, import java util,

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

Text link code :
Hyper link code:

Also see ...

Article In : Computers & Technology  -  java