aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Question 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 Question" Watch "Thread Question" New topic
Author

Thread Question

Mike Cutter
Ranch Hand

Joined: Jun 09, 2002
Posts: 49
I saw this thread question in a mock exam.
public class T {
public synchronized void a() {}
public void b() {
synchronized(this) {}
}
public void c(Object o) {
synchronized(o) {}
}
public void d() {}
}
Given t of type T, which two statements are true?
A. Multiple threads can invoke t.a() concurrently.
B. While a thread is invoking t.a(), no other thread may invoke t.b().
C. Multiple threads can invoke t.d() concurrently.
D. Multiple threads can invoke the synchronized block in t.c().
E. While a thread is invoking the synchronized block in t.c(), no other thread may invoke t.a(), t.b(), or t.d().
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
C & D


SCJP Tipline, etc.
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
Heh, nevermind. I was gonna ask a question about D, but I saw the answer immediately after posting this.

C - t.d() isn't synchronized, so multiple threads can run this concurrently.
D - the Object o passed to t.c() can be different objects, so the synchronized block can be run multiple times concurrently.
[ June 18, 2002: Message edited by: Paul Villangca ]
arnold yan
Ranch Hand

Joined: May 31, 2002
Posts: 40
Can someone explain to me why answer B is not correct? Thnaks.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
arnold_yan,
Welcome to Javaranch
We'd like you to read the Javaranch Naming Policy and change your publicly displayed name to comply with our unique rule. Thank you.
With regards to your question, however, answer B is incorrect because the synchronization occurs within the method. Therefore, any number of threads may call that method, but all but one will have to wait once inside for the previous thread to complete, due to the synchronized block.
Corey
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Thread Question