JAVA THREADS FREQUENTLY ASKED QUESTIONS AND ANSWERS
THREADS
Question:
What is the difference between yielding and sleeping?
Answer:
When a task invokes its yield() method, it returns to the ready state. When a
task invokes its sleep() method, it returns to the waiting state.
Question:
When a thread blocks on I/O, what state does it enter?
Answer:
A thread enters the waiting state when it blocks on I/O.
Question:
When a thread is created and started, what is its initial state?
Answer:
A thread is in the ready state after it has been created and started.
Question:
What invokes a thread's run() method?
Answer:
After a thread is started, via its start() method or that of the Thread class,
the JVM invokes the thread's run() method when the thread is initially
executed.
Question:
What method is invoked to cause an object to begin executing as a separate
thread?
Answer:
The start() method of the Thread class is invoked to cause an object to begin
executing as a separate thread.
Question:
What is the purpose of the wait(), notify(), and notifyAll()
methods?
Answer:
The wait(),notify(), and notifyAll() methods are used to provide an efficient
way for threads to wait for a shared resource. When a thread executes an
object's wait() method, it enters the waiting state. It only enters the ready
state after another thread invokes the object's notify() or notifyAll()
methods.
Question:
What are the high-level thread states?
Answer:
The high-level thread states are ready, running, waiting, and dead
Question:
What happens when a thread cannot acquire a lock on an object?
Answer:
If a thread attempts to execute a synchronized method or synchronized statement
and is unable to acquire an object's lock, it enters the waiting state until
the lock becomes available.
Question:
How does multithreading take place on a computer with a single CPU?
Answer:
The operating system's task scheduler allocates execution time to multiple
tasks. By quickly switching between executing tasks, it creates the impression
that tasks execute sequentially.
Question:
What happens when you invoke a thread's interrupt method while it is sleeping
or waiting?
Answer:
When a task's interrupt() method is executed, the task enters the ready state.
The next time the task enters the running state, an InterruptedException is
thrown.
Question:
What state is a thread in when it is executing?
Answer:
An executing thread is in the running state
Question:
What are three ways in which a thread can enter the waiting state?
Answer:
A thread can enter the waiting state by invoking its sleep() method, by
blocking on I/O, by unsuccessfully attempting to acquire an object's lock, or
by invoking an object's wait() method. It can also enter the waiting state by
invoking its (deprecated) suspend() method.
Question:
What method must be implemented by all threads?
Answer:
All tasks must implement the run() method, whether they are a subclass of
Thread or implement the Runnable interface.
Question:
What are the two basic ways in which classes that can be run as threads may be
defined?
Answer:
A thread class may be declared as a subclass of Thread, or it may implement the
Runnable interface.