S.No: 24 Exp.
Name: Write a Java program demonstrating the usage of Threads Date: 2022-06-15
ID: 20095A0363
Page No:
Aim:
Write a Java program that uses three threads to perform the below actions:
1. First thread should print "Good morning" for every 1 second for 2 times
2. Second thread should print "Hello" for every 1 seconds for 2 times
3. Third thread should print "Welcome" for every 3 seconds for 1 times
Write appropriate constructor in the Printer class which implements Runnable interface to take three
arguments : message, delay and count of types String, int and int respectively.
Write code in the Printer.run() method to print the message with appropriate delay and for number of
times mentioned in count.
Write a class called ThreadDemo with the main() method which instantiates and executes three instances
of the above mentioned Printer class as threads to produce the desired output.
[Note: If you want to sleep for 2 seconds you should call Thread.sleep(2000); as the
Thread.sleep(...) method takes milliseconds as argument.]
Note: Please don't change the package name.
Source Code:
q11349/ThreadDemo.java
Rajeev Gandhi Memorial College of Engineering and Technology (Autonomous) 2019-2023-MECH-FDH
package q11349;
public class ThreadDemo {
public static void main(String[] args) throws Exception {
Thread t1 = new Thread(new Printer("Good morning", 1, 2));
Thread t2 = new Thread(new Printer("Hello", 1, 2));
Thread t3 = new Thread(new Printer("Welcome", 3, 1));
t1.start();
t2.start();
t3.start();
t1.join();
t2.join();
t3.join();
System.out.println("All the three threads t1, t2 and t3 have completed exe
cution.");
class Printer implements Runnable {
String message;
int delay;
int count;
Printer(String message,int count,int delay){
this.message=message;
this.delay=delay;
this.count=count;
public void run(){
int time=count*1000;
try{
Thread.sleep(time);
for(int i = 0;i<delay;i++) {
System.out.println(message);
ID: 20095A0363
Page No:
}
catch(InterruptedException ie) {
System.out.println(ie);
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Good morning
Hello
Welcome
Good morning
Hello
All the three threads t1, t2 and t3 have completed execution.
Rajeev Gandhi Memorial College of Engineering and Technology (Autonomous) 2019-2023-MECH-FDH