Multithreaded Java Programming - Quiz Explanation
The answers you selected are indicated below, along with text that explains the correct answers.
1.
To start the execution of a thread after you create it, you must:
Please select the best answer.
A.
Call the
run()
method
B.
Call the
start()
method
C.
Do nothing--threads start automatically upon creation
The correct answer is B.
To start the execution of a thread after you create it, you must call the
start()
method. The
run()
method is called automatically and contains the main code for a thread; you never directly call this method. You must always call the
start()
method to start the execution of a newly created thread.
2.
What method in the
Thread
class do you call to make the current thread cease executing for a specified amount of time?
Please select the best answer.
A.
sleep()
B.
pause()
C.
stop()
The correct answer is A.
The
sleep()
method in the
Thread
class is called to pause a thread. There is no method defined in the
Thread
class named
pause()
. The
stop()
method in the
Thread
class is deprecated and should never be used to stop a thread.
3.
You designate a method as being synchronized by:
Please select the best answer.
A.
A. Using the
synchronized
keyword
B.
B. Placing a synchronized section of code within it
C.
C. Naming the method
synchronize
The correct answer is A.
You designate a method or a section of code as being synchronized by using the
synchronized
keyword. A synchronized section of code within a method does not make the entire method synchronized. And the name of a method has no bearing on it being synchronized.
Java Concurrency
4.
What is the primary drawback to using synchronized methods?
Please select the best answer.
A.
They take longer to compile than unsynchronized methods.
B.
They compile into larger executable bytecode than unsynchronized methods.
C.
They are less efficient than unsynchronized methods.
The correct answer is C.
The primary drawback to using synchronized methods is that they are less efficient than unsynchronized methods. Synchronized methods have no special impact on the compilation process or the resulting bytecode generated