Multi Threading Java Interview Questions – Set 10

Explain different ways of creating a thread Threads can be used by either: Extending the Thread class. Implementing the Runnable interface. Using the Executor framework (this creates a thread pool) By extends: class Counter extends Thread { //method where the thread execution will start public void run(){ //logic to execute in a thread } //let’s see how to start the … Read more

Multi Threading Java Interview Questions – Set 09

Why is locking of a method or block of code for thread safety is called “synchronized” and not “lock” or “locked When a method or block of code is locked with the reserved “synchronized” key word in Java, the memory (i.e. heap) where the shared data is kept is synchronized. This means,When a synchronized block … Read more

Multi Threading Java Interview Questions – Set 08

What is the difference between sleep(), suspend() and wait() Thread.sleep() takes the current thread to a “Not Runnable” state for specified amount of time. The thread holds the monitors it has acquired. For example, if a thread is running a synchronized block or method and sleep method is called then no other thread will be … Read more

Multi Threading Java Interview Questions – Set 07

How will you fix the above racing issue This can be fixed a number of ways. Option 1: Method level synchronization. This is the simplest. As you can see, the increment() method is synchronized, so that the other threads must wait for the thread that already has the lock to execute that method. import java.util.HashMap; import … Read more

Multi Threading Java Interview Questions – Set 06

What can prevent the execution of the code in finally block ? and what are the rules for catching multiple exceptions? The death of thread – Use of system.exit() – Turning off the power to CPU – An exception arising in the finally block itself Rules for catching multiple exceptions – A more specific catch … Read more

Multi Threading Java Interview Questions – Set 05

What method is invoked to cause an object to begin executing as a separate thread? The start() method of the Thread class is invoked to cause an object to begin executing as a separate thread. Why do threads block on I/O? Threads block on I/O (that is enters the waiting state) so that other threads may execute while the … Read more

Multi Threading Java Interview Questions – Set 04

What invokes a thread’s run() method After a thread is started, via its start() method or that of the Thread class, the JVM invokes the thread’srun() method when the thread is initially executed. When a thread is created and started, what is its initial state A thread is in the ready state after it has been created and started. How … Read more

Multi Threading Java Interview Questions – Set 03

What is garbage collection The runtime system keeps track of the memory that is allocated and is able to determine whether that memory is still useable. This work is usually done in background by a low-priority thread that is referred to as garbage collector. When the gc finds memory that is no longer accessible from … Read more

Multi Threading Java Interview Questions – Set 02

What is synchronization and why is it important? With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one thread to modify a shared object while another thread is in the process of using or updating that object’s value. This often … Read more

Multi Threading Java Interview Questions – Set 01

What is the wait/notify mechanism? This deals with concurrent programming. The wait() and notify() methods are designed to provide a mechanism to allow a thread to be block until a specific condition is met. However, java.util.concurrent should be used instead of wait() and notify() to reduce complexity. How can a collection object be sorted? // … Read more