Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Experiment No-8

Name: Sonali Suryavanshi

PRN:2110057

Course:JAVA Programming Lab

Topic: Multithreading

Problem Statements:
1) Write a Java program to create the thread by extending the Thread class
class Multi extends Thread
{

public void run()


{
System.out.println("Thread is running!!!");
}

public static void main(String args[])


{
Multi m1=new Multi();
m1.start();
}
}
Output:

2) Write a Java program to create the thread by implementing the Runnable Interface.
class Multiple implements Runnable
{
public void run()
{
System.out.println("Thread is running!!");
}
public static void main(String args[])
{
Multiple m2=new Multiple();
Thread t=new Thread(m2); // Using the constructor Thread(Runnable r)
t.start();
}
}
Output:

3) Write a java program to create the thread.


Using Thread class: Thread(String Name)
public class Mythread{
public static void main(String args[]){
Thread m=new Thread("My first thread");

m.start();

String str=m.getName();
System.out.println(str);

}
}
Output:

Using Thread Class: Thread(Runnable r,String name)


public class Mythread1 implements Runnable
{
public void run()
{
System.out.println("Now the thread is running...");
}
public static void main(String args[])
{
Runnable r=new Mythread1();
Thread t=new Thread(r,"My first thread");
t.start();

String str=t.getName();
System.out.println(str);
}
}

Output:

4) Show demonstration of properties of the thread like setname, setpriority, getname, getpriority etc.
class ThreadDemo extends Thread
{
public void run()
{
System.out.println("Inside thread..");
}

public static void main(String args[])


{
ThreadDemo t1=new ThreadDemo();
ThreadDemo t2=new ThreadDemo();
ThreadDemo t3=new ThreadDemo();

System.out.println("t1 thread priority: "+t1.getPriority());


System.out.println("t2 thread priority: "+t2.getPriority());
System.out.println("t3 thread priority: "+t3.getPriority());

System.out.println("Currently executing thread: "+Thread.currentThread().getName());


System.out.println("Main thread priority: "+Thread.currentThread().getPriority());

Thread.currentThread().setPriority(10);
Thread.currentThread().setName("high");
System.out.println("Main thread priority: "+Thread.currentThread().getName());
System.out.println("Main thread priority: "+Thread.currentThread().getPriority());

}
}
Output:

5) Write a program to print "Good morning" and "TY-CSIT" continuously on the screen in Java using
threads. (create two threads for these task)
class PrintGoodMorning extends Thread
{
public void run()
{
while (true)
{
System.out.println("Good morning");
try
{
Thread.sleep(1000); // Sleep for 1 second
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}

class PrintTYCSIT extends Thread


{
public void run()
{
while (true)
{
System.out.println("TY-CSIT");
try
{
Thread.sleep(1000); // Sleep for 1 second
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}

public class Main


{
public static void main(String[] args)
{
// Creating two threads
PrintGoodMorning goodMorningThread = new PrintGoodMorning();
PrintTYCSIT tycsitThread = new PrintTYCSIT();

// Starting the threads


goodMorningThread.start();
tycsitThread.start();
}
}

Output:
6) Write a java program to demonstrate the different state of Thread.
class ThreadStateDemo extends Thread
{
public void run()
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}

public static void main(String[] args)


{
ThreadStateDemo thread = new ThreadStateDemo();

// NEW state
System.out.println("Thread State: " + thread.getState());

// START the thread


thread.start();

// RUNNABLE state
System.out.println("Thread State: " + thread.getState());

try
{
thread.join(); // Wait for the thread to finish
}
catch (InterruptedException e)
{
e.printStackTrace();
}

// TERMINATED state
System.out.println("Thread State: " + thread.getState());
}
}

Output:

You might also like