When InvalidMonitorStateException is thrown? Why

This exception is thrown when you try to call wait()/notify()/notifyAll() any of these methods for an Object from a point in your program where u are NOT having a lock on that object.(i.e. u r not executing any synchronized block/method of that object and still trying to call wait()/notify()/notifyAll()) wait(), notify() and notifyAll() all throw … Read more

What is ThreadLocal Class? How can it be Used

Below are some key points about ThreadLocal variables A thread-local variable effectively provides a separate copy of its value for each thread that uses it. ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread In case when multiple threads access a ThreadLocal instance, separate copy of Threadlocal … Read more

Why do we Need run() & start() method both. Can we Achieve it with Only Run Method

We need run() & start() method both because JVM needs to create a separate thread which can not be differentiated from a normal method call. So this job is done by start method native implementation which has to be explicitly called. Another advantage of having these two methods is we can have any object run … Read more

What is the Difference Between Thread.start() & Thread.run() Method

Thread.start() method (native method) of Thread class actually does the job of running the Thread.run() method in a thread. If we directly call Thread.run() method it will executed in same thread, so does not solve the purpose of creating a new thread. In Java, the Thread.start() and Thread.run() methods are related to the execution of … Read more

Explain Different way of Using Thread

A Java thread could be implemented by using Runnable interface or by extending the Thread class. The Runnable is more advantageous, when you are going for multiple inheritance. In Java, there are two main ways to create and use threads: Extending the Thread class: You can create a new class that extends the Thread class … Read more