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 ??
Joined: Jun 19, 2002
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.