Thread Priority, yield(), join() & sleep() Questions

Java Multithreading Interview Questions Part – 3 | In the previous Java Multithreading Interview Questions Part – 2 we have seen interview questions related to defining thread (start(), run(), getState(), isAlive(), setName(), & getName()).

Thread Priority

Q1) What is thread priority?
Every thread in JVM is created with a priority value which is called thread priority.

Q2) What is the default priority of every new thread?

The default priority of the new thread is inherited from the parent thread. For example:- the default value of the main thread is 5, if a new thread is created by the main thread then the newly created thread having priority = 5.

Q3) Why do most of the threads have default priority 5?

Most of the threads are created by the main thread. The default priority of the main thread is 5, therefore by default the priority of the child thread will be 5.

Q4) What is the range of thread priority?
The valid range of thread priority is 1 to 10.

Q5) How can we get & set the priority of a thread?

PrepBytes
Use Code KNOWPROGRAM10 to get FLAT 10% OFF on all Courses

Using getPriority() and getPriority(int p) methods. While calling the getPriority() method the passed value must belong to 1 to 10.

Q6) What will happen if we set thread priority to 20?
We will get Runtime error:- java.lang.IllegalArgumentException

Q7) If two threads have different priority then which thread will execute first?
The thread having high priority will get a chance first to execute.

Q8) If two threads have the same priority, which thread will execute first?

If two threads are having the same priority then their execution order will be decided by the thread scheduler, we can’t expect anything.

Q9) When a new thread is born, how can we start execution immediately?
Assign higher priority compared to parent and sibling threads.


Preventing thread from Execution (yield(), join() and sleep() Methods)

Q10) What are the different ways to pause thread Execution?***

Different ways to prevent or stop a thread execution temporarily (not permanently) are:-

  1. yield() method
  2. join() method
  3. sleep() method

Q11) What is the purpose of the yield() method?***

The yield() method pauses the currently executing thread to give the chance for waiting threads of the same priority. 

  • If there are no waiting threads or all waiting threads have low priority then the same thread can continue its execution.
  • If multiple threads are waiting with the same priority then which waiting thread will get a chance:- we can’t expect, it depends on the thread scheduler.
  • The thread which is yielding, when it will get the chance back it also depends on the thread scheduler and we can’t expect exactly. 

Q12) When should we use the sleep() method? 

If a thread doesn’t want to perform any operation for a particular amount of time then the sleep() method is used.

Q13) When should we use the join() method?

If a thread wants to wait until the completion of some other thread then we should go for the join() method. 

Q14) How can a thread interrupt another thread?
By using the interrupt() method.

Q15) What happens after calling the interrupt() method if the target thread is not in a waiting or sleeping state? 

When we call the interrupt() method and the target thread is not in a waiting or sleeping state then there will be no impact of interrupt call immediately. The interrupt calls will wait till the target thread enters into a waiting or sleeping state. 

  1. If the target thread entered into the waiting or sleeping state:- Then an interrupt call will immediately interrupt the target thread, InterruptedException will be raised. Interrupt calls will not be wasted. 
  2. If the target thread never entered into the waiting or sleeping state in its lifetime:- Then interrupt call will be wasted and no impact of interrupt call.

Q16) If the interrupt() method is called on the child thread by the main thread, then will the main thread wait till the child thread gets interrupted?

public class Test {
   public static void main(String[] args) {
      MyThread t = new MyThread();
      t.start();
      t.interrupt();
      // other work
   }
}

No, the main thread will not wait, it will continue its execution. Interrupting the thread is the task of JVM. This job will be done by JVM, the main thread will not wait, it will continue its operation.

Q17) Difference between yield() and join() method?***

If a thread wants to pause its execution to give the chance for remaining threads of the same priority then the yield() method is used. But if a thread wants to wait until the completion of some other thread then the join() method is used. 

The yield() method doesn’t contain any overload form, doesn’t throw Exception whereas the join() method contains three overloaded forms, and all of them throw InterruptedException. The yield() method is native, and static whereas join() method is final, instance nature defined in Java (not native).

Q18) What is the difference between yield() and sleep() method?

The yield() method is used If a thread wants to pause its execution to give the chance for remaining threads of the same priority. Whereas the sleep() method is used If a thread doesn’t want to perform any operation for a particular amount of time.

Q19) What is the difference between join(), join(1000), and sleep(1000)?

  • If the join() method is called on the child thread by the main thread then the main thread will wait till execution completion of the child thread. 
  • But if the join(1000) method is called on the child thread by the main thread then the main thread will wait a maximum 1000 milliseconds. If within 1000 ms child thread execution is not completed then the main thread will come out of the waiting state and try to start its own execution. 
  • When sleep(1000) is called then the compulsory thread has to wait 1000 ms after that it will try to start its own execution.

Q20) Comparison table for yield(), join() and sleep() Method of thread class?

Propertiesyield()join()sleep()
Purpose (When should we use it?)We should use the yield() method if a thread wants to pause its execution to give the chance for remaining threads of the same priority.If a thread wants to wait until the completion of some other thread then we should go for the join() method.We should use the sleep() method If a thread doesn’t want to perform any operation for a particular amount of time.
Is it overloaded?NoYesYes
Is it final?NoYesNo
Is it throwing an InterruptedException?NoYesYes
Is it Native?YesNosleep(long ms) => native;

sleep(long ms, int ns) => non-native
Is it static?YesNoYes
Mern Stack Web Development
Use Code KNOWPROGRAM10 to get FLAT 10% OFF on all Courses

Leave a Comment

Your email address will not be published. Required fields are marked *