• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronization

 
Eric Lidell
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the following question i picked up in the internet.
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);
}
}
if the code is run with assertions enabled ,what are the possible answers?
a.The code will fail to compile.
b.Seperate threads can execute up() concurrently
c.Seperate threads can execute down() concurrently
d.Seperate threads can execute both up() & down() concurrently
e.the assertion wont fail under any circumstance.
What is the possible answer and the reason?
 
Jason Menard
Sheriff
Posts: 6450
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simon Templar,
We don't have many rules at JavaRanch, but one that we do have is a naming convention. If you would be a saint and kindly change your display name to conform, it would be most appreciated.
Thanks,
Jason (moderator)
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm moving this to The Threads and Synchronization Forum...
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic