Thursday, September 12, 2013

Thread : 04 Basic contd. [implements Runnable Interface]


Thread Basic contd. [implements Runnable Interface]

/**
* @author sanjeeva
*
* Implementing Runnable interface
* - If we are not extending the Thread class, class object would not be treated as a thread object.
* So need to explicitely create Thread class object.
* in here : Thread thread = new Thread(threadRunnable);
*/
public class ThreadRunnableExample implements Runnable {

       public static void main(String[] args) {
               ThreadRunnableExample threadRunnable = new ThreadRunnableExample();
               Thread thread = new Thread(threadRunnable);
                thread.start();
       }

       @Override
       public void run() {
               displayMessage();
       }
       private void displayMessage(){
               System.out.println("Thread is running...");
               System.out.println("Thread Name : " + Thread.currentThread().getName());
               System.out.println("Thread Priority : " + Thread.currentThread().getPriority());
               System.out.println("Thread is Daemon : " + Thread.currentThread().isDaemon());
               System.out.println("Thread is Alive : " + Thread.currentThread().isAlive());
       }
}


Thread is running...
Thread Name : Thread-0
Thread Priority : 5
Thread is Daemon : false
Thread is Alive : true