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