Home Tutorial Java Thread Main Thread and Child Thread

 
 

Share on Google+Share on Google+
Main Thread and Child Thread
Posted on: November 7, 2009 at 12:00 AM
Advertisement
In this segment of tutorial we will learn about the Main Thread and Child Thread.Then We will create an example for Main thread and Child Thread

  • There are two types of threads in Java Progarm
    • In Java there are Main and Child Threads used in Programming.
  • Main thread is automatically created when program runs.
  • Child Thread gets created by the main thread .


Java Main Thread Example

public class mainchild implements Runnable {
	Thread t1;

	public mainchild(String a) {
		t1 = new Thread(this, a);
		t1.start();
	}

	public void run() {
		for (int x = 1; x <= 3; x++) {
			System.out.println(x + "  this thread is "
					+ Thread.currentThread().getName());
		}
	}

	public static void main(String[] args) {
		mainchild mch = new mainchild("Child ");
		for (int x = 1; x <= 3; x++) {
			System.out.println(x + "  this thread is "
					+ Thread.currentThread().getName());
		}
	}
}

Output

1 this thread is main 1 this thread is Child 2 this thread is Child 3 this thread is Child 2 this thread is main 3 this thread is main
Advertisement

Related Tags for Main Thread and Child Thread:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 7, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com