DevPartner Java 3.0 and Out-Of-Order Deadlock Detection

0 Likes

Problem:

Is there sample code which will demonstrate the Out-Of-Order Deadlock Detection functionality when profiled?

Resolution:

/*

* Synchronization test that uses multiple synchronized blocks within the

* same class, but which synchronize on different objects.

* The methods are called twice, once in order ABCD, then in order

* DCB. Note that, between calls, object A is 'released'. (It's only

* referrer is nulled, and a garbage collection is run.)

*/

import java.lang.Thread;

import java.util.*;

import java.io.*;

public class SynchBlocks3 {

Object a;

Object b;

Object c;

Object d;

/*

* RunTests()

*/

public void RunTests()

{

a = new Object();

b = new Object();

c = new Object();

d = new Object();

sync1(true);

thing();

a = null;

System.gc();

sync4(false);

return;

}

/*

* Sync1

*/

public void sync1(boolean j)

{

synchronized(a)

{

if(j)

sync2(j);

}

return;

}

/*

* Sync2

*/

public void sync2(boolean j)

{

synchronized(b)

{

if(j)

sync3(j);

else

// sync1(j);

return;

}

return;

}

/*

* Sync3

*/

public void sync3(boolean j)

{

synchronized(c)

{

if(j)

sync4(j);

else

sync2(j);

}

return;

}

public void sync4(boolean j)

{

synchronized(d)

{

if(!j)

sync3(j);

}

return;

}

/*

*

*/

public void thing()

{

System.out.println('thing called');

return;

}

public static void main(String[] args) {

// Run the tests

SynchBlocks3 nestTest = new SynchBlocks3();

nestTest.RunTests();

System.out.println('DONE!');

System.exit(0);

}

}

Old KB# 11854
Comment List
Anonymous
Related Discussions
Recommended