Java Multithreading Interview Questions and Answers

41. What does sleep() method when it is called on a thread?

Ans: The sleep() method is a static method provided by the Thread class. It is used to sleep a thread for a specified amount of time.

When it is called on a thread, sleep() method pauses the current thread for a given period of time. The current thread is become out of the queue and enters into a blocked (or non-runnable state) for a specified amount of time.

When the specified amount of time is elapsed, the thread goes into the runnable state (ready state).

42. How to sleep current thread in Java?

Ans: Refer to this tutorial: Java Thread sleep.

43. What is yield in Java?

Ans: When a currently executing thread goes to the runnable state from running state, this process is called yield in Java.

44. What happens when yield() method is called on a thread object?

Ans: When the yield() method is called on a thread object, it does not move the currently running thread into sleeping, waiting, or blocking state. It sends the thread into the runnable state (ready state).

45. What does join() method in Java?

or, What is the purpose of join() method in Thread class?

Ans: The join() method is an instance method that is always called by an object of Thread class. It waits for a thread to die. In other words, it causes to wait for the current thread until the thread that has called the join() method completes its execution.

46. Which exception is thrown by join() method when it is interrupted?

Ans: InterruptedException. It must be enclosed in a Java try-catch block.

47. What is the fundamental difference between yield() and sleep() methods?

Ans: The main differences between yield() and sleep() methods are as follows:

a) When yield() method is called on a thread, it gives an indication to the thread scheduler that current thread is willing to yield its current use of a processor.

When sleep() method is called, it pauses the current thread for a specified amount of time.

b) When sleep() method is invoked on a thread, the thread goes from running state to waiting state and can return to runnable state when sleep time is over.

When yield() method is called, the thread goes from running state to runnable state, not in waiting state.

c) yield() method stops the thread for an unpredictable amount of time that depends on the thread scheduler. But sleep() method sleeps thread for the specified amount of time.

d) yield() method does not need to catch or throw any exception. But sleep method must catch or thrown InterruptedException.

48. What is the minimum number of threads in a Java program?

Ans: JVM executes each Java program within the main process that starts with java.exe. Therefore, each Java program has at least one thread.

49. What is synchronization in Java?

Ans: Synchronization in Java is the ability (or feature) to control the access of multiple threads to any shared resource. It is used:

  • To prevent thread interference.
  • To prevent consistency problem.
  • To prevent deadlock between multiple threads.

50. What is thread synchronization (or thread-safe) in Java?

Ans: When a thread is already accessing an instance of a class, and preventing any other thread from acting on the same instance is called ‘thread synchronization in Java‘ or ‘ Thread safe‘ in Java.

51. What is a synchronized object in Java?

Ans: The object on which a thread is synchronized is called synchronized object.

52. What is the purpose of thread synchronization in Java?

Ans: The purpose (objective) of thread synchronization is to control the use of any shared resource.

53. What is a monitor in Java?

Ans: A monitor is an object lock that can block and resume threads. It allows only one thread to use the shared resource (object) at a time.

54. How to acquire an object lock on a thread in Java?

Ans: To acquire an object lock on a thread, we need to call a method or a block with the synchronized keyword.

55. What is static synchronization in Java?

Ans: We define a static method as synchronized in Java, the lock will be on the class, not on object.

56. How can we achieve Synchronization in Java?

Ans: There are two ways by which we can achieve or implement synchronization in Java. They are:

  • Synchronized Method
  • Synchronized Block

For more detail, go to this tutorial: Synchronized method in Java

57. Can we synchronize static method in Java?

Ans: Yes, a static method can also be synchronized. In this case, the lock is placed on the class, not on the object.

58. What is a Race condition? 

Ans: A race condition is an unwanted situation in which multiple threads access the same object and modify the state of object inconsistently.

Race condition can be solved with the help of synchronization mechanism in which when one thread is accessing the state of object, the other threads will wait to access the same object at a time until their come turn.

59. What is a Deadlock in Java?

Ans: A deadlock is a situation that occurs when two or more threads are waiting on each other to release a resource. Each thread is waiting for a resource that is held by the other waiting thread.

60. What happens when thread deadlock occurs in a program?

Ans: When thread deadlock occurs in a program, the further execution of the program will stop. We should take care to avoid deadlock while coding.