| Refresh | Home EGTry.com

create a thread class by extend Thead, and run thead by calling start method


ThreadExt.java

/*
 * Copyright@ 2011 www.egtry.com
 */

package egtry.thread;

///create a thread object by extends Thread
public class ThreadExt extends Thread {

  public static void main(String[] args) {
	  System.out.println("Main Thread at the beginning: "+Thread.currentThread().getId());
	  ThreadExt t1=new ThreadExt();
	  t1.start();
	  System.out.println("Main Thread at the end: "+Thread.currentThread().getId());
  }
  
  @Override
  public void run() {
		System.out.println("Thread started");
		System.out.println("Child Thread: "+Thread.currentThread().getId());
  }
}



Output

Main Thread at the beginning: 1
Main Thread at the end: 1
Thread started
Child Thread: 8