public class MyThread 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)
{
doIt();
check();
}
}
void doIt()
{
synchronized(lock1){i1++; }
j1++;
synchronized(lock2){k1++; k2++; }
j2++;
synchronized(lock1){i2++; }
}
void check()
{
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[] as)
{
new MyThread().start();
new MyThread().start();
}
}
The answer is "One cannot be certain whether any of the letters i, j, k will be printed during execution."
Can anyone explain me how?
Thanks
kanchan