Explain different ways of creating a thread. Which one would you prefer and why ?
There are three ways that can be used in order for a Thread
to be created:
- A class may extend the
Thread
class. - A class may implement the
Runnable
interface. - An application can use the
Executor
framework, in order to create a thread pool.
Explain the available thread states in a high-level. During its execution, a thread can reside in one of the following status:
NEW
: The thread becomes ready to run, but does not necessarily start running immediately.RUNNABLE
: The Java Virtual Machine (JVM) is actively executing the thread’s code.BLOCKED
: The thread is in a blocked state while waiting for a monitor lock.WAITING
: The thread waits for another thread to perform a particular action.TIMED_WAITING
: The thread waits for another thread to perform a particular action up to a specified waiting time.TERMINATED
: The thread has finished its execution.