• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread :(

 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the following code, which statements are true?Select 2.

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);
}
}



A.The code will fail to compile
B.Separate thread can execute the up() method concrrently.
C.Separate thread can execute the dwn() method concrrently.
D.Separate thread can execute the up() and down() method concrrently.
E.The assertion in jump() will not fail under any circumstances.

I am still confused about how synchronized method works to decide on an answer
 
Wirianto Djunaidi
Ranch Hand
Posts: 210
Ruby Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
synchronized method indicate that any threads that tries to execute the method on the same instance must get a lock first on the instance before it can execute it, which mean at any time there can be only 1 thread executing the method on a specific instance.

I hightlight the specific instance because any threads can execute concurently if they are calling the same method on different instances of the object.

With that in mind I don't think the question that you posted is an example of good question, because the choices are vague.

A.The code will fail to compile
>> this is false because the code will compile
B.Separate thread can execute the up() method concrrently.
>> this is vague, seperate thread can execute the up() method concurently if it calls different instances of Vertical.
C.Separate thread can execute the dwn() method concrrently.
>> this is true because down() is not synchronized, so there is no limitation on concurrency.
D.Separate thread can execute the up() and down() method concrrently.
>> B && C resulted to this to be FALSE
E.The assertion in jump() will not fail under any circumstances.
>> This is also false. Even though jump is synchronized and call down() inside it, it does not prevent other thread to call down() directly. Which in this regard the assertion might fail under certain circumstances.

Since you said said to select 2 and there is only 1 true statement in the choices, this is a bad question.
[ September 10, 2008: Message edited by: Wirianto Djunaidi ]
 
victor kamat
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a) is wrong because this code compiles.
b) is wrong because separate threads cannot execute the up() method concurrently -- it is synchronized.
c) is correct since separate threads can execute the the down() method concurrently -- it is not synchronized.
d) is correct since separate threads can execute the the down() method concurrently -- the up() method but the down() is not.
e) is wrong since two separate threads may concurrently execute the up() and down() methods ( see c ).

That answers the question.

As to how synchronization works this may help.
When a method is synchronized only 1 thread may execute the method.
If a mehtod is not synchronized more 2+ threads may execute the method.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15452
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nabila Mohammad, when you copy a question from a book or mock exam, we require that you quote your sources. So, please tell us where you copied this from.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry ,I forgot.
This is from Whizlab.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks , Wirianto and Victor

That was a great Explanation!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic