Monday, January 20, 2014

Java Program to illustrate MultiThreading

with 0 Comment
import java.io.*;
class multithread implements Runnable
{
String name;
Thread t;
multithread(String n)
{
name=n;
t=new Thread(this,name);
System.out.println("Thread:"+t);
t.start();
}
public void run()
{
try
{
for(int i=1;i<=5;i++)
{
System.out.println(name+":"+i);
Thread.sleep(1000);
}
System.out.println(name+"Thread is Excited");
}
catch(InterruptedException ie)
{
System.out.println("ChildProgram is Interrupted:"+ie);
}
}
}
class multithreadDemo
{
public static void main(String args[])
{
multithread t1=new multithread("First");
multithread t2=new multithread("Second");
try
{
for(int i=5;i>=0;i--)
{
System.out.println("Main Thread:"+i);
Thread.sleep(3000);
}
}
catch(InterruptedException ie)
{
System.out.println("Main Thread is Interrupted:"+ie);
}
System.out.println("Main Thread is excited");
}
}

-------------
OUTPUT:
-------------
Thread :Thread[First,5,main]
Thread :Thread[Second,5,main]
First:1
MainThread:5
Second:1
First:2
Second:2
Second:3
First:3
MainThread:4
First:4
Second:4
Second:5
First:5
SecondThread is Excited
FirstThread is Excited
MainThread:3
MainThread:2
MainThread:1
MainThread:0
MainThread is Excited

0 comments:

Post a Comment

Powered by Blogger.

Blog Archive