Home »
Java programs
Java program to demonstrate example of thread
This example is based on threading in java, here we will learn how to use thread in java?
Submitted by BalRam Dixit, on July 02, 2017
In this example, there are two loops which are running within the run () method, by using this java example, we can understand the concept of threading, how thread runs in java?
Java program to demonstrate example of thread
Consider the program
public class ThreadCreateDemo implements Runnable{
public static void main(String[] ar){
ThreadCreateDemo tcd1=new ThreadCreateDemo();
ThreadCreateDemo tcd2=new ThreadCreateDemo();
Thread t1=new Thread(tcd1);
Thread t2=new Thread(tcd2);
t1.start();
t2.start();
}
public void run(){
for(int i=0;i<=5;i++){
System.out.println("i "+i);
}
for(int j=0;j<=5;j++){
System.out.println("j "+j);
}
}
}
Output
i 0
i 1
i 2
i 3
i 4
i 5
j 0
j 1
j 2
j 3
j 4
j 5
i 0
i 1
i 2
i 3
i 4
i 5
j 0
j 1
j 2
j 3
j 4
j 5