Beginners always confused in writing a Java Java Program to Demonstrate Multithreading - Java Program of Multithreading, So here i am providing you the full and easy solution of the Java Program of Multithreading:
Ques : Program to demonstrate multithreading.
Sol. :
Java Program of Multithreading
class ThreadDemo implements Runnable
{
String str;
ThreadDemo(String s)
{
str=s;
}
public void run()
{
try{
for(int i=1;i<=5;i++)
{
System.out.println(str+” “+i);
}
Thread.sleep(500);
}
catch(InterruptedException ie)
{
System.out.println(ie.toString());
}
}
}
class ThreadTest
{
public static void main(String s[])
{
Thread t1, t2, t3;
ThreadDemo td1=new ThreadDemo(“First”);
ThreadDemo td2=new ThreadDemo(“Second”);
ThreadDemo td3=new ThreadDemo(“Third”);
t1 = new Thread(td1);
t2 = new Thread(td2);
t3 = new Thread(td3);
t1.start();
t2.start();
t3.start();
}
}
OUTPUT of Java Program of Multithreading
First 1
Second 1
Second 2
Second 3
Second 4
Second 5
First 2
Third 1
First 3
Third 2
First 4
Third 3
First 5
Third 4
Third 5
Software to use - Java Program of Multithreading
Simply Use NOTEPAD OR TEXTPAD because Java is an Case sensitive Language and use cmd to compile and Run your Program.