Saturday, January 12, 2013

ThreadStartExample


package com.multithreading;
public class ThreadStartExample extends Thread{

public static void main(String args[]) throws InterruptedException {

ThreadStartExample t1 = new ThreadStartExample();
t1.setName("firstThread");

t1.start();/*
if START() IS not CALLED than newly created thread(BY EXTENDING THREAD.......) also runs within main thread
*/
t1.run();



Thread t2 = new Thread(new ThreadByRunnable(),"second");

t2.start();
/*
if START() IS not CALLED than newly created thread (BY IMPLEMENTATION OF RUNNABLE)also runs within main thread i.e. in different stack
*/
t2.run();


}

public void run(){
System.out.println("Thread running is..............."+Thread.currentThread().getName());
}

}

class ThreadByRunnable implements Runnable{

public void run() {
System.out.println("Running thread when implemanted runnable is............."+Thread.currentThread().getName());
}



}

No comments:

Post a Comment