Threads in Java
Overview When a program is executing it follows a path, that is Thread. Threads are used to perform critical tasks in the background without interrupting the main thread. There are two ways to create a Thread. By extending the class Thread By implementing an interface Runnable Five states of Thread Born Runnable Running Non Runnable Dead sleep() vs wait() sleep() belongs to Thread class whereas wait() belongs to an object. sleep() can be called without a synchronized context but wait() should be called in a synchronized context. sleep() does not release lock on an object but wait() releases lock on an object during synchronization. sleep() is a static method but wait() is not a static method. sleep() is executed on a Thread but wait() method is executed on an object. interrupt() This method is used to interrupt an executing Thread. If a Thread is in sleep() or wait() state then the interrupt() method will get called and the catch() statement gets executed. If a Thread doesn't go i...