Home Tutorial Java Thread Main Thread and Child Thread

 
 

Main Thread and Child Thread
Posted on: November 7, 2009 at 12:00 AM
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

Related Tags for Main Thread and Child Thread:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.