0% found this document useful (0 votes)
5 views3 pages

Threads PracticePrograms

The document contains five Java programs demonstrating different aspects of threading. Program 1 shows thread creation by extending the Thread class, while Program 2 demonstrates thread creation using the Runnable interface. Programs 3, 4, and 5 illustrate the use of the sleep method, thread naming, and thread priority, respectively.

Uploaded by

Satyasri Viswa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Threads PracticePrograms

The document contains five Java programs demonstrating different aspects of threading. Program 1 shows thread creation by extending the Thread class, while Program 2 demonstrates thread creation using the Runnable interface. Programs 3, 4, and 5 illustrate the use of the sleep method, thread naming, and thread priority, respectively.

Uploaded by

Satyasri Viswa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Program-1

-----------
Creation of thread by extending Thread class
class MyThread extends Thread
{
public void run()
{
for (int i = 1; i <= 5; i++)
{
[Link]("Thread is running: " + i);
try
{
[Link](500);
}
catch (InterruptedException e)
{
[Link](e);
}
}
}
}

public class ThreadDemo


{
public static void main(String[] args)
{

MyThread t1 = new MyThread();


[Link]();

for (int i = 1; i <= 5; i++)


{
[Link]("Main thread: " + i);
try
{
[Link](500);
}
catch (InterruptedException e)
{
[Link](e);
}
}
}
}

Program-2
-----------
Creation of thread by implementing Runnable interface
class MyRunnable implements Runnable
{
public void run()
{
for (int i = 1; i <= 5; i++)
{
[Link]("Thread is running: " + i);
try
{
[Link](500);
}
catch (InterruptedException e) {
[Link](e);
}
}
}
}

public class RunnableDemo


{
public static void main(String[] args)
{
MyRunnable myTask = new MyRunnable();
Thread t1 = new Thread(myTask);
[Link]();

for (int i = 1; i <= 5; i++)


{
[Link]("Main thread: " + i);
try
{
[Link](500);
}
catch (InterruptedException e)
{
[Link](e);
}
}
}
}

Program-3
-----------
Program to demonstrate sleep()
class TestSleepMethod extends Thread
{
public void run()
{
for(int i=1;i<5;i++)
{
try
{
[Link](3000);
}
catch(InterruptedException e)
{
[Link](e);
}
[Link](i);
}
}
public static void main(String args[])
{
TestSleepMethod t1=new TestSleepMethod();
TestSleepMethod t2=new TestSleepMethod();
TestSleepMethod t3=new TestSleepMethod();
[Link]();
[Link]();
[Link]();
}
}

Program-4
-----------
Program to demonstrate getName() and setName()
class TestMultiNaming extends Thread
{
public void run()
{
[Link]("running...");
}
public static void main(String args[])
{
TestMultiNaming t1=new TestMultiNaming();
TestMultiNaming t2=new TestMultiNaming();

[Link]("Name of t1:"+[Link]());
[Link]("Name of t2:"+[Link]());

[Link]();
[Link]();

[Link]("Thread1");
[Link]("After changing Name of t1:"+[Link]());
}
}

Program-5
-----------
Program to demonstrate thread priority
class TestMultiPriority extends Thread
{
public void run()
{
[Link]("running thread name is:"+[Link]().getName());

[Link]("running thread priority


is:"+[Link]().getPriority());
}
public static void main(String args[])
{
TestMultiPriority m1=new TestMultiPriority();
TestMultiPriority m2=new TestMultiPriority();
[Link](Thread.MIN_PRIORITY);
[Link](Thread.MAX_PRIORITY);
[Link]();
[Link]();
}
}

You might also like