File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question from Khalid book

 
kanchan chaudhary
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 213
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic