Java Multithreading Fundamentals - Quiz Explanation

The answers you selected are indicated below, along with text that explains the correct answers.
 
1. A single sequence of code executing within a program is known as:
Please select the best answer.
  A. A method
  B. A process
  C. A thread
  The correct answer is C. A single sequence of code executing within a program is known as a thread. Although a method certainly contains a sequence of code, the code may or may not be executing as a single sequence unto itself. A process typically refers to an application executing as a whole, and not a singular sequence of code.


2. There are three Threads trying to invoke a method of an object. This method is a synchronized method to prevent conflict between Threads.
While Thread1 is executing the method, Thread2 and Thread3, in that order attempt to invoke the method.
What happens when Thread1 exits the method?
Please select the best answer.
  A. Thread2 will be the first waiting Thread to execute the method.
  B. Thread3 will be the first waiting Thread to execute the method.
  C. The order in which the Thread2 and Thread3 will invoke the method cannot be determined.
  C is correct.
When threads wait for an object's monitor, they are in the blocked state. They move into the Runnable state when the lock is released.
The order in which they will be chosen for running cannot be determined because the thread scheduler does not consider the order in which threads attempt to access a locked object. Thus choices A, B, are incorrect.

3. What method is declared in the Runnable interface and serves as the path of execution for all threads?
Please select the best answer.
  A. start()
  B. run()
  C. go()
  The correct answer is B.
The run() method is declared in the Runnable interface and serves as the path of execution for all threads. The start() method isn't declared in the Runnable interface.
There is no go() method declared in the Runnable interface.


Java Concurrency