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++; }
synchronized(lock2) { k1++ ; k2++ ; }
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();
}
}
Answer given is:
One cannot be certail whether any of the letters i,j and k will be printed during execution.
While I agree with this,I also feel that the other option which states that "One can be certain that the letters i and k will never be printed" is also true as i and k are incremented together.Please clarify.
Regards,
Rashmi