Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

marcus mock exam-----thread

 
chao-long liao
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which of the following statements about threading are true
1) You can only obtain a mutually exclusive lock on methods in a class that extends Thread or implements runnable
2) You can obtain a mutually exclusive lock on any object
3) A thread can obtain a mutually exclusive lock on an object by calling a synchronized method of that object.
Answer are 2 and 3,but why ??
 
Amir Ghahrai
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer 1 is incorrect since you can synchronize an object or a method even without extending the Thread class or implementing the Runnable interface.
Answer 2 and 3 are true, since all java object have locks. i.e. you can synchronize any java object or method by the use of the keyword synchronized. this mutually exclusive locking mechanism comes from the fact, that only one thread at a time is able to execute a block of code that is synchronized.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following thread also discusses this issue:
http://www.coderanch.com/t/238510/java-programmer-SCJP/certification/threads
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic