0% found this document useful (0 votes)
3 views

LABMANUAL_p7_1

The document contains a Java program demonstrating multithreading with a main thread and a child thread. The main thread counts down from 5, while the child thread also counts down from 5 at a different interval. Both threads print their respective counts and handle interruptions appropriately.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

LABMANUAL_p7_1

The document contains a Java program demonstrating multithreading with a main thread and a child thread. The main thread counts down from 5, while the child thread also counts down from 5 at a different interval. Both threads print their respective counts and handle interruptions appropriately.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

class ThreadDemo {

public static void main(String args[])


{
new NewThread(); // create a new thread
try
{
for(int i = 5; i > 0; i--)
{
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
class NewThread implements Runnable {
Thread t;
NewThread()
{
t = new Thread(this, "Demo Thread");
System.out.println("Child thread: " + t);
t.start(); // Start the thread
}
public void run()
{
try
{
for(int i = 5; i > 0; i--)
{
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
}
catch (InterruptedException e)
{
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}

You might also like