• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Question

 
Mike Cutter
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C & D
 
Paul Villangca
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone explain to me why answer B is not correct? Thnaks.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic