Which of the following statements is not true about threads?

  1. If the start() method is invoked twice on the same Thread object, an exception is thrown at runtime.

  2. The order in which threads were started might differ from the order in which they actually run.

  3. If the run() method is directly invoked on a Thread object, an exception is thrown at runtime.

  4. If the sleep() method is invoked on a thread while executing synchronized code, the lock is not released.


Correct Option: C
Explanation:

To answer this question one needs to know about threads in programming. Threads are the smallest unit of execution that can be scheduled by an operating system. Each thread represents a separate flow of control. Threads are lightweight compared to processes, and they share memory with other threads in the same process.

Now let's go through each option and explain why it is true or false:

A. If the start() method is invoked twice on the same Thread object, an exception is thrown at runtime.

This statement is true. If the start() method is invoked twice on the same Thread object, an exception (IllegalThreadStateException) is thrown at runtime.

B. The order in which threads were started might differ from the order in which they actually run.

This statement is true. The operating system schedules threads based on its own algorithm, which might differ from the order in which the threads were started.

C. If the run() method is directly invoked on a Thread object, an exception is thrown at runtime.

This statement is false. If the run() method is directly invoked on a Thread object, the run() method will execute just like any other method. However, it will not start a new thread of execution, and any other code that is waiting for the thread to complete will not be notified.

D. If the sleep() method is invoked on a thread while executing synchronized code, the lock is not released.

This statement is true. When a thread invokes the sleep() method while executing synchronized code, the lock associated with the synchronized code is not released. This means that other threads that require the same lock will have to wait until the sleeping thread releases the lock.

Therefore, the correct answer is: C. If the run() method is directly invoked on a Thread object, an exception is thrown at runtime.

Find more quizzes: