This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question from Khalid book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question from Khalid book" Watch "Question from Khalid book" New topic
Author

Question from Khalid book

kanchan chaudhary
Greenhorn

Joined: Jun 06, 2001
Posts: 28
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
Charlie Swanson
Ranch Hand

Joined: Jan 29, 2001
Posts: 213
I believe the reason is due to the synchronization. Since you have the same object running, the variables will also be shared.
In addition, note how the locks will be given. A lock will be given, and then released. Right after it is release, the other thread can access the same lock, and update the static variable that is shared. But, we do not know for sure that this will happen. It is only a possibility.
kanchan chaudhary
Greenhorn

Joined: Jun 06, 2001
Posts: 28
So, it is only a guess, So, one cannot also be sure whether the answer(in exam) is correct or wrong.
sleep(long), wait(long), & yield(), out of these 3 methods, which releases the monitor when the thread goes in waiting state? wait() releases the monitor & regains it when other thread notifies it, but what about wait(long) or wait(long, int)?
I am giving the exam on 8th June.
So, reply sooner will be helpful.
Thanks
kanchan
Kevin Yip
Ranch Hand

Joined: Oct 17, 2000
Posts: 110
The current thread must own this object's monitor when any of all the three versions of wait() is called.
You can check the API documentation.
kanchan chaudhary
Greenhorn

Joined: Jun 06, 2001
Posts: 28
That's correct, but can U pls tell me that when a thread executes sleep(), or yield() in a synchronized method, can another thread enter the synchronized method?
Thanks
kanchan
Bin Wang
Ranch Hand

Joined: May 01, 2001
Posts: 82

Originally posted by kanchan chaudhary:
The answer is "One cannot be certain whether any of the letters i, j, k will be printed during execution."

I have a doubt here. I think letter k will never get a chance to be printed during execution because k1 != k2 will never return ture (which means k1 and k2 will always have the same value at any given time). Can somebody explain this?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question from Khalid book