Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads

 
vinita Kh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question from Marcus Green-test-II
nebody explain why option 1 is false
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 a synchronized method of an object
4) Thread scheduling algorithms are platform dependent
 
Gautam Sewani
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1 is false coz u can obtain a mutually exclusive lock on ANY OBJECT,by just calling one of its synchronized methods!
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every object in Java has its own "lock." You can obtain that lock by calling a synchronized instance method on that object or by beginning a synchronized block which locks on that object.
Similarly, each class has its own "lock." You can obtain this lock by invoking a synchronized static method or by beginning a synchronized block which locks on that class.
As the class lock and the object lock are entirely separate, it is possible for one thread to be executing an instance method on an object while another thread executes a static method of the class of that object. It is, however, impossible for two or more threads to be executing more than one synchronized instance method (or block) and similarly for static methods (and blocks).
Check out the JLS, §17.13 Locks and Synchronization for more details.
I hope that helps,
Corey
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic