See more articles about "java "

Tutorial Addendum On Java - Deadlocks



 31 December 18:00   

    



    



    

Deadlock Sample Program - Appointment Funds

    



    

Another analogy of the deadlock problem is the afterward program,

    

which simulates assorted accoutrement demography affairs to alteration funds

    

among altered accounts.

    

 

    

/**

    

* FundTransfer.java

    

* Absorb (c) 2002 by Dr. Yang

    

*/

    

import java.util.*;

    

public chic FundTransfer {

    

accessible changeless final int numberOfAccounts = 50; // amount of accounts

    

accessible changeless long antithesis = new long;

    

accessible changeless Accidental randomGenerator = new Random();

    

clandestine changeless final int numberOfThreads = 10;

    

clandestine changeless TransferThread threadList =

    

new TransferThread;

    

clandestine changeless Object lockList = new Object;

    

accessible changeless abandoned main(String a) {

    

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

    

lockList = new Object();

    

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

    

balance = 100000;

    

ThreadGroup g = new ThreadGroup("The Group");

    

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

    

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

    

t.start();

    

threadList = t;

    

}

    

System.out.print("
Number of affairs performed per thread:");

    

continued accomplish = 0;

    

while (true) {

    

steps++;

    

System.out.print("
Step="+steps+": ");

    

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

    

System.out.print(threadList.counts+" ");

    

try {

    

Thread.sleep(200);

    

} bolt (InterruptedException e) {

    

System.out.println("Interrupted.");

    

}

    

}

    

}

    

accessible changeless boolean doTransfer(int i, int j, continued a) {

    

boolean ok = false;

    

synchronized (lockList) {

    

continued c = balance; // get antithesis of the from-account

    

ok = c-a>=0;

    

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

    

try {

    

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

    

} bolt (InterruptedException e) {

    

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

    

}

    

synchronized (lockList) {

    

continued d = balance; // get antithesis of the to-account

    

ok = d+a>=0;

    

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

    

balance = c-a; // amend the from-account

    

balance = d+a; // amend the to-account

    

}

    

}

    

}

    

}

    

acknowledgment ok;

    

}

    

accessible changeless chic TransferThread extends Cilia {

    

accessible continued counts;

    

accessible TransferThread(ThreadGroup g, Cord n) {

    

super(g,n);

    

counts = 0;

    

}

    

accessible abandoned run() {

    

while (!isInterrupted()) {

    

counts++;

    

Accidental r = randomGenerator;

    

int m = numberOfAccounts;

    

int i = r.nextInt(m); // from-account number

    

int j = r.nextInt(m); // to-account number

    

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

    

continued a = (long) t*r.nextInt(10000);

    

if (i!=j) doTransfer(i,j,a);

    

try {

    

sleep(r.nextInt(200)); // abeyance amid transactions

    

} bolt (InterruptedException e) {

    

break;

    

}

    

}

    

}

    

}

    

}

    



    



    

The argumentation of the program is simple. The capital cilia launches 10 sub accoutrement at the beginning,

    

and allows anniversary cilia to again alteration some armamentarium from a about selected

    

account to addition about called account. The capital cilia keeps checking

    

the amount of affairs anniversary cilia has performed so far.

    



    

If it happens that one cilia is appointment armamentarium from annual A to B, and

    

another cilia is appointment armamentarium from annual B to A at the aforementioned time,

    

the two accoutrement will become deadlock to anniversary other. Then, if addition thread

    

is aggravating to alteration armamentarium from annual C to A or B, it will be put on hold

    

also, because A and B are both locked. Annual C will be bound aswell in this

    

case. If addition cilia is aggravating to alteration armamentarium from A or B to D, it will

    

also be put on hold, but annual D will not be locked. Slowly, all threads

    

will be put on hold, because of the two apoplectic threads.

    



    

Execute the program several times. You will see that it behaves abnormally

    

each time. But anon or later, the all accoutrement will be put on authority as predicted.

    

One of my executions gave the afterward output:

    

 

    

Number of affairs performed per thread:

    

Step=1: 0 0 0 0 0 0 1 1 1 1

    

Step=2: 2 2 2 4 3 3 2 2 6 2

    

Step=3: 5 3 4 6 4 6 3 4 10 4

    

Step=4: 6 5 5 8 6 11 4 6 13 6

    

Step=5: 8 8 8 11 8 14 5 7 14 7

    

Step=6: 9 10 10 13 9 16 7 10 15 8

    

Step=7: 11 12 12 14 12 18 10 12 18 11

    

Step=8: 14 14 15 17 13 20 12 14 21 12

    

Step=9: 15 15 17 19 17 22 14 17 23 15

    

Step=10: 18 16 19 20 18 23 16 21 24 18

    

......

    

Step=813: 1613 1652 1607 1605 1627 1606 1613 1623 1601 1647

    

Step=814: 1616 1653 1611 1606 1630 1608 1615 1624 1604 1649

    

Step=815: 1617 1656 1615 1608 1631 1610 1616 1626 1607 1651

    

Step=816: 1619 1658 1617 1613 1633 1611 1619 1627 1608 1653

    

Step=817: 1621 1660 1617 1614 1633 1613 1621 1629 1610 1655

    

^1 ^2

    

Step=818: 1622 1660 1617 1615 1633 1614 1623 1632 1612 1656

    

^3

    

Step=819: 1623 1660 1617 1615 1633 1615 1625 1634 1613 1657

    

^4

    

Step=820: 1624 1660 1617 1615 1633 1617 1627 1636 1615 1659

    

Step=821: 1624 1660 1617 1615 1633 1617 1629 1638 1617 1662

    

^5 ^6

    

Step=822: 1624 1660 1617 1615 1633 1617 1630 1640 1621 1664

    

Step=823: 1624 1660 1617 1615 1633 1617 1631 1641 1624 1666

    

Step=824: 1624 1660 1617 1615 1633 1617 1632 1643 1626 1667

    

Step=825: 1624 1660 1617 1615 1633 1617 1632 1645 1627 1668

    

^7

    

Step=826: 1624 1660 1617 1615 1633 1617 1632 1647 1628 1668

    

^8

    

Step=827: 1624 1660 1617 1615 1633 1617 1632 1647 1628 1668

    

^9 ^10

    



    



    

As you can see, accoutrement 3 and 5 became apoplectic at move 817.

    

Then aural 10 steps, all additional 8 accoutrement were stopped.

    



    



 


 thread, account, balance, public, static, threads, transferthread, numberofaccounts, nextint, transactions, system, locklist, transfer, numberofthreads, steps, counts, program, interruptedexception, locked, catch, sleep, object, private, random, transferring, threadlist, deadlock, print, threadgroup, string, performed, , 1660 1617, 1617 1615, 1615 1633, 1633 1617, 1624 1660, public static, fund from, 1617 1632, system out, catch interruptedexception, private static, 1617 1615 1633, 1660 1617 1615, 1624 1660 1617, 1615 1633 1617, 1633 1617 1632, fund from account, 1647 1628 1668, 1632 1647 1628, 1617 1632 1647, stop accout balance, balance going negative, transferring fund from, transfer fund from, numberofaccounts public static, accout balance going,

Share Tutorial Addendum On Java - Deadlocks:
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 - Beheading Ambiance
System PropertiesJava appliance programs are accomplished central a Java Basic Apparatus (JVM), which is in turnexecuted central an Operating Arrangement (OS). From the Java applianc

Tutorial Addendum On Java - Beheading Ambiance
Non system PropertiesThe arrangement backdrop are infact stored in a map structure. It can aswell be acclimated by the Java appliance program to abundance its own properties. For example, t

Tutorial Addendum On Java - Beheading Ambiance
The program seems to be confusing:Why do I charge two altered methods to retrieve the anamnesis information?Why are the methods alleged again 5 times?Why are the pres

Tutorial Addendum On Java - Beheading Ambiance
Output: D:\write_20021217java_20021217semoryUsageMemory acceptance afore arrangement allocation: Chargeless anamnesis = 1777248 Absolute anamnesis = 2031616 Best anamnesis =

Tutorial Addendum On Java - Beheading Of Java Programs
Execution ProcessA Java appliance program is create of one or added classes and aught or added interfaces. One of the chic musthave the main() adjustment as the beheading star

Tutorial Addendum On Java - Beheading Of Java Programs
Execution Access PointAs mentioned in the antecedent section, a Java appliance program haveto accept a starting chic with a appropriate adjustment alarm main() as the beheading acces

Tutorial Addendum On Java - Beheading Of Java Programs
Execution ConsoleWhen the JVM is active a Java appliance program, it needs a animate windowto provides 3 predefined ascribe and achievement streams to the Java program: to:br

Tutorial Addendum On Java - Synchronization
Why SynchronizationThe better problem of acceptance assorted accoutrement administration the aforementioned data setis that one operation in one cilia could bang with addition operation

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