Fichas
Threads
Fichas del conjunto (7)
- 1What are Threads?A process is a program in execution. A thread is a subset of a process.
- 2How do you make a thread in java? or how do you create thread?In Java, we can create a thread using
By Extending the Thread class
By Implementing Runnable interface in Java - 3What is the life cycle of a thread?At any given time, a thread can be in one of these states:
New: newly created thread that has not started executing
Runnable: either running or ready for execution but waiting for its resource allocation
Blocked: waiting to acquire a monitor lock to enter or re-enter a synchronized block/method
Waiting: waiting for some other thread to perform an action without any time limit
Timed_Waiting: waiting for some other thread to perform a specific action for a specified time period
Terminated: has completed its execution - 4.How can we differentiate notify() and notifyAll() methods ?notify(): It sends a notification and wakes up only a single thread instead of multiple threads that are waiting on the object’s monitor.
notifyAll(): It sends notifications and wakes up all threads and allows them to compete for the object's monitor instead of a single thread. - 5Explain synchronization process? Why we use it?Synchronization in java is the capability to control the access of multiple threads to any shared resource. In the Multithreading concept, multiple threads try to access the shared resources at a time to produce inconsistent results. The synchronization is necessary for reliable communication between threads.
- 6Explain thread starvation?Thread starvation is basically a situation or condition where a thread won’t be able to have regular access to shared resources and therefore is unable to proceed or make progress. This is because other threads have high priority and occupy the resources for too long. This usually happens with low-priority threads that do not get CPU for its execution to carry on.
- 7Can you start a thread twice?No, it's not at all possible to restart a thread once a thread gets started and completes its execution. Thread only runs once and if you try to run it for a second time, then it will throw a runtime exception i.e., java.lang.IllegalThreadStateException.