This week's book giveaway is in the HTML/CSS/JavaScript forum.
We're giving away four copies of Practical SVG and have Chris Coyier on-line!
See this thread for details.
Win a copy of Practical SVG this week in the HTML/CSS/JavaScript 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
 
Oh the stink of it! Smell my tiny ad!
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!