Threading in Java refers to the concept of running multiple pieces of work or threads simultaneously within a single Java program. Java provides a built-in threading model that allows developers to easily create and manage threads within their code. Threads can be used to improve the performance and responsiveness of Java programs, and are particularly useful for applications that require real-time processing or input from users. Some of the key concepts in Java threading include the Thread class, Runnable interfaces, and thread synchronization.
class NewThread extends Thread
{
NewThread()
{
System.out.println("Child Thread :"+ this);
start();
}
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");
}
}
class Thread3
{
public static void main (String args[])
{
new NewThread();
try
{
for (int i = 5; i > 0 ; i--)
{
System.out.println("Main Thread:"+ i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println("Main Threa Intrrupted");
}
System.out.println("Main Thread Exiting");
}
}