Skip to main content

Posts

Showing posts with the label Join

Java Thread States

Java Threads may have 6 states: new , runnable , terminated , blocked , waiting , timed_waiting . When a thread is created it is in new state. When start method of thread is called it enters runnable state. Runnable state has two inner states: ready and running . If thread is eligible for execution it is said to be ready, if it is executing it is in running state. Remember calling start method on a already started thread will raise IllegalThreadStateException. When thread finishes its execution it enters into terminated state. When a thread is trying to access a resource, a synchronized statement for example, and it is not available, lock of the object is already acquired for example, it is blocked and said to be in blocked state. When lock is released an thread has chance to acquire lock it goes back to runnable state. When a thread calls join or wait method it enters into waiting state. When joined thread finishes or for wait method notify/notifyAll metho...

Thread Basics

Thread Basics 1-       Thread Creation : There are two ways of creating a new thread. Way-1 Implementing Runnable Interface: public class CustRunnable implements Runnable {     public void run () {         System . out . println ( "Executed !" );     }     public static void main ( String args []) {         Thread myThread =   new Thread ( new CustRunnable ());         myThread . start ()     } } Way-2 Extending Thread Interface: public class CustThread extends Thread {     public void run () {         System . out . println ( "Executed from a new thread!" );     }     public static void main ( String args []) {      ...