Why Thread?

Here is a quick brief about a concept of thread. Thread is a light weight process, it takes less time to create, context switch or destroy thread than a process. We want simultaneous activities for a better interraction with a user or take advantages of multi-processors to archieve a maximum system resourse utilization.

For instance, in word processor, one thread responses for I/O, while another is doing a grammar check. Now, lets take a look at a simple JAVA program to see how the thread create, run, and how threads assignment work in JAVA.

public class thread extends Thread{
public void run() {
for(int i = 0; i < 10; i++) {
System.out.println("Child thread " + i);

public static void main(String[] args) {
thread t = new thread();

for(int i = 0; i < 10; i++) {
System.out.println("Parent thread " + i);
}// end main
}// end class thread

In the code above, we created two threads. When the new thread create, it will go to the run() method and do the job, so we can see from the output from the program that there will be two threads execute concurrently.

program output:

Parent thread 0
Parent thread 1
Child thread 0
Parent thread 2
Child thread 1
Parent thread 3
Child thread 2
Parent thread 4
Child thread 3
Parent thread 5
Child thread 4
Parent thread 6
Child thread 5
Parent thread 7
Child thread 6
Parent thread 8
Child thread 7
Parent thread 9
Child thread 8
Child thread 9