Java Thread Interview Questions and Answers

    6 Votes

Frequently asked Advanced Java Thread and Java Exceptions Interview Questions and Answers for 2-6 year experienced Java developers, Senior software engineers, Tech Lead and freshers.

1) What is meant by Thread ?

A thread is a path of execution of a Program. A Thread is a sequence of instructions that executed in a predefined unique flow of control. Advantages of thread are

  • Reduces development time
  • Reduces maintenance costs
  • Improves the performance of complex applications
  • Useful for improving the responsiveness of the user interfaces
  • Used in server applications for improving high throughput and resource utilization

2) Explain Thread Life Cycle ?

Thread Life cycle have four states as below

  • New - While thread is initialized, it will enter to init state. After initialization, we should call start() method. If start method is not called, java throws IllegalThreadStateException. 
  • Runnable - Once a start method is called then it will enter to runnable state. Resources and schedules associated with the thread is allocated by start method. Then it will call run() method.
  • Not Runnable  
    • sleep() - Wait for specified time
    • wait() - Wait until notify.
    • suspend() - Blocked by another thread
  • Dead - if the run method gets completed or thread object is assigned a null value, its enter into dead state.
 

3) What are the differences between Thread and process ?

ProcessThread
It has own address. Share the address space of the process.
It has their own copy of the data segment of the parent process. It have direct access to the data segment of its process.
It must use interprocess communication to communicate with sibling processes. Threads can directly communicate with other threads of its process
Changes to the parent process do not affect child processes. Changes to the main thread like cancellation or change in priority of thread may affect the behaviour of the other threads of the process

4) Is it possible to restart the dead Thread in Java ?

No. A thread can't be restarted once the execution is completed.

5) Can you explain about Synchronized keyword in Java ?

When 2 or more threads try to access a shared object, Java throws thread interference and memory consistency errors. To overcome this scenario, we need to synchronize java objects using Synchronized keyword. We can use synchronize keyword in 

  • Used in method - public void synchronized method { // access code}
  • Used in block - synchronized(obj) { // access code}


6) How object are communicate with each other in synchronized context ?

In all the classes, final methods like wait, notify and notifyAll has been implemented. All three methods can be called only from within a synchronized context. Using these methods object are communicated with each other in synchronized context.

7) What is the difference between sleep() and wait() ?

Sleep()Wait()
Thread stops working for the specified duration. Thread stops working until the object in waiting is notified.
Thread method. Object method
It doesn’t release the lock while waiting. It releases lock on object while waiting.
Put thread on sleep It is normally done on condition.

8) How to stop a running thread in Java ?

Thread will stop once come out of run method.  So one of the way to stop a thread as follows

public class MyThread extends Thread {
 @Override
 public void run() {
    while (!this.isInterrupted()) {
    }
  }
}
// To stop a thread, call method interrupt():
myThread.interrupt();

9) What is the difference between notify() and notifyAll() ?

notifynotifyAll
Used to wake up a single thread All threads in wait state wakes up.
Page 1 of 4

Popular Videos

How to speak to people

How to speak so that people want to listen.

Got a tip or Question?
Let us know

Related Articles

Java OOPS Interview Questions and Answers
Java Interview Questions
Top Advanced Java Interview Questions and Answers
Java Collections Interview Questions and Answers