Which of the following statements is not true about threads ?
Choices:
- A. If the start() method is invoked twice on the same Thread object, an exception is thrown at runtime.
- B. The order in which threads were started might differ from the order in which they actually run.
- C. If the run() method is directly invoked on a Thread object, an exception is thrown at runtime.
- D. If the sleep() method is invoked on a thread while executing synchronized code, the lock is not released.
Correct choice:
- C
Explanation:
Statement C is the correct answer; it is not true about threads. If the run() method is directly invoked on a Thread object, no exception is thrown at runtime. However, the code written in the run() method would be executed by the current thread, not by a new thread. So the correct way to start a thread is by invoking the start() method, which causes the run() method to be executed by the new thread. However, invoking the start() method twice on the same Thread object will cause an IllegalThreadStateException to be thrown at runtime, so A is true.
B is true because the order in which threads run is decided by the thread scheduler, no matter which thread was started first. D is true because a thread will not relinquish the locks it holds when it goes into the sleeping state.
No comments:
Post a Comment