Program:-
class
Threaddemo implements Runnable
{
Thread t;
Threaddemo(String name)
{
t = new
Thread(this,name);
System.out.println("Curent
Thread: "+t);
t.start();
}
public void run()
{
try
{
for(int
i=0;i<10;i++)
{
if(i%2!=0)
{
System.out.println("Odd
Thread: "+ i);
Thread.sleep(500);
}
}
}catch(Exception e)
{
System.out.println("Thread
Interrupted... "+e);
}
System.out.println("Child
thread exiting.");
}
}
class u71
{
public static void main(String ar[])
{
new
Threaddemo("Number");
Thread t1 =
Thread.currentThread();
System.out.println("Current
thread: " + t1);
try
{
for(int
i=0;i<10;i++)
{
if(i%2==0)
{
System.out.println("Even
Thread: "+ i);
Thread.sleep(500);
}
}
}catch(Exception e)
{
System.out.println("Thread
Interrupted... "+e);
}
System.out.println("Main thread
exiting.");
}
}
No comments:
Post a Comment