File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread :( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread :(" Watch "Thread :(" New topic
Author

Thread :(

Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
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


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210

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

Joined: Jan 10, 2007
Posts: 247
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

Joined: Aug 16, 2005
Posts: 13875
    
  10

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
I am sorry ,I forgot.
This is from Whizlab.
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Thanks , Wirianto and Victor

That was a great Explanation!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread :(
 
Similar Threads
Thread
Dumb question about threads #2
synchronization
synchronisation problem
mock question from khalid Moughal