Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronisation problem

 
mridul das
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is a question from the khalid mock test
Given the following code, which statements are true?
public class Vertical {
private int alt;
public synchronized void up() {
++alt;
}
public void down() {
--alt;
}
public synchronized void jump() {
int a = alt;
up();
down();
assert(a == alt);
}
}
options were
1)The code will fail to compile
2)seprate threads can execute the up() method concurrently
3)seprate threads can execute the down() method concurrently
4)seprate threads can execute both the up() and down() methods concurrently
5)the assertion in the jump() method will not fail under any circumstances
Answers were 3 and 4.
i couldnt get how can up() method be accessed by two threads when it is synchronised.Please clear the doubt.
 
Warren Dew
blacksmith
Ranch Hand
Posts: 1332
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Synchronization of a function only prevents simultaneous execution of other synchronized functions and blocks. Unsynchronized code can still run without restrictions.
 
mridul das
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still havent got my doubt cleared.The method down() is not synchronised so it can be accessed by more than a single thread concurrently.But how is method up() which is synchronised be accessed by more than one thread concurrently.
 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method down() is not synchronised so it can be accessed by more than a single thread concurrently


I agree multiple threads can concurrently access the method down(). The answer is number 3.

But how is method up() which is synchronised be accessed by more than one thread concurrently.


I don't believe it can. But in my opinion the answer is in how the correct question is worded...
4)seprate threads can execute both the up() and down() methods concurrently


Let's say we have 2 distinct seperate threads T1 and T2.
Thread T1 acquires the object lock and proceeds to call the method jump(), which contains a call to the synchronized method up() and then the un-synchronized method down(), during the same time, Thread T2 accesses the method down() directly. So in essence, you have 2 seperate threads accessing the methods up() and down() concurrently.

Just my opinion.

[ July 20, 2005: Message edited by: Craig Jackson ]
[ July 20, 2005: Message edited by: Craig Jackson ]
 
mridul das
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have modified the Vertical program to test the concurrency as
class Vertical {
private int alt;
public synchronized void up() {
System.out.println(Thread.currentThread().getName()+" executing up() "+alt);
try{
Thread.sleep(1000);
}
catch(InterruptedException e){}
++alt;
System.out.println(Thread.currentThread().getName()+" ending up() "+alt);
}
public void down() {
--alt;
}
public synchronized void jump() {
int a = alt;
up();
down();
}
}
to see if the synchronised method up() is being executed by two threads concurrently
now to execute this i wrote:

public class test{
public static void main(String[] args){
final Vertical v = new Vertical();
Runnable r = new Runnable(){
public void run(){
while(true){
v.up();v.down();v.jump();
}
}
};
Thread T1 = new Thread(r);
Thread T2 = new Thread(r);
T1.start();
T2.start();
}
}
when i saw the results i got this:
Thread-1 executing up() 0
Thread-1 ending up() 1
Thread-2 executing up() 1
Thread-2 ending up() 1
Thread-1 executing up() 0
Thread-1 ending up() 1
Thread-2 executing up() 0
Thread-2 ending up() 1
Thread-1 executing up() 0
Thread-1 ending up() 1
Thread-2 executing up() 1
Thread-2 ending up() 1
Thread-1 executing up() 1
....
there is no concurrent execution of up(). but if I changed the code in Vertical to include the System.out.... stmts in non synchronized down() i got:
Thread-1 executing down() 1
Thread-2 executing down() 2
Thread-1 ending down() 1
Thread-1 executing down() 2
Thread-2 ending down() 1
Thread-1 ending down() 0
Thread-2 executing down() 1
Thread-2 ending down() 0
Thread-1 executing down() 1
Thread-2 executing down() 2
Thread-2 ending down() 1
Thread-2 executing down() 2
Thread-1 ending down() 1
Thread-2 ending down() 0
Thread-1 executing down() 1this is being executed by two thread T1 and T2.as seen from first two output lines.
i dont think the answers given by Khalid are correct .Please clearify
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by mridul das:

4)seprate threads can execute both the up() and down() methods concurrently


This is poorly worded. So much so that I would say it is incorrect. If this is to be true should remove the word 'both' as a minimum. Otherwise it looks like its asking if both statements 2 and 3 are true.

[ July 24, 2005: Message edited by: CL Gilbert ]
[ July 24, 2005: Message edited by: CL Gilbert ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic