Question ID :952739431860
Given the following program, which one of these statements is true?
public class TestClass extends
Thread {
static Object lock1 = new Object();
static Object lock2 = new Object();
static volatile int i1, i2, j1, j2, k1, k2;
public void run()
{
while (true)
{
workWithLocks();
workWithoutLocks();
}
}
void workWithLocks()
{
synchronized(lock1) { i1++ ; i2++; } // Line 1
synchronized(lock2) { k1++ ; k2++ ; } //Line 2
j1++; j2++;
}
void workWithoutLocks()
{
if (i1 != i2) System.out.println("i");
if (j1 != j2) System.out.println("j");
if (k1 != k2) System.out.println("k");
}
public static void main(
String args[])
{
new TestClass().start();
new TestClass().start();
}
}
I can not understand the meaning of Line 1 & Line 2.What i know about synchronized statements/block is that, the object's reference pass as a parameter in synchronized block, is the only object which has monitor.No other object can access that code within synchronized block.
In the above pgm composition is done.Every object of TestClass will have two objects of Object class.And access is allowes to these objects i.e lock1 & lock2.And NOT to the object of TestClass.
This is what my concept.I know it may be wrong.Please clarify me this.
Bye.
Viki.
------------------
Count the flowers of ur garden,NOT the leafs which falls away!