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

Experiment_13_

The document outlines a laboratory exercise for Object Oriented Programming using Java, focusing on multithreading concepts. It includes several coding tasks such as creating a multithreaded program to print multiplication tables, display patterns using child threads, generate Fibonacci sequences, and manage ticket booking with thread synchronization. Additionally, it demonstrates various thread methods like wait, notify, and priority settings.

Uploaded by

Mohammad Shaikh
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)
5 views

Experiment_13_

The document outlines a laboratory exercise for Object Oriented Programming using Java, focusing on multithreading concepts. It includes several coding tasks such as creating a multithreaded program to print multiplication tables, display patterns using child threads, generate Fibonacci sequences, and manage ticket booking with thread synchronization. Additionally, it demonstrates various thread methods like wait, notify, and priority settings.

Uploaded by

Mohammad Shaikh
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/ 7

Object Oriented Programming using Java Laboratory (DJS23FLES201) Academic

Year 2024-25
Name- Mohammad Ahmed Shaikh
Sap id – 60003240302
Roll no-I169

13. To implement Multithreading (CO1, CO2)


a. Write a multithreaded program a java program to print Table of Five, Seven and Thirteen
using Multithreading (Use Thread class for the implementation).

Code: class EXP13a extends


Thread {

private int number; public


EXP13a(int number) { this.number
= number;

}
public void run() {
System.out.println("Table of " + number + ":");
for (int i = 1; i <= 10; i++) {

System.out.println(number + " x " + i + " = " + (number * i));


}
System.out.println();
}

public static void main(String[] args) {


EXP13a t5 = new EXP13a(5);

EXP13a t7 = new EXP13a(7); EXP13a


t13 = new EXP13a(13);

t5.start(); t7.start(); t13.start();


}
}
Object Oriented Programming using Java Laboratory (DJS23FLES201) Academic Year
2024-25

Output:

b. Write a multithreaded program to display /*/*/*/*/*/*/*/* using 2 child threads.


Object Oriented Programming using Java Laboratory (DJS23FLES201) Academic Year
2024-25

Code: class EXP13b extends


Thread {

public void run() { for (int i


= 0; i < 4; i++) {

System.out.print("/*");
}
}

public static void main(String[] args) {


EXP13b t1 = new EXP13b();
EXP13b t2 = new EXP13b();
t1.start();

t2.start();
}
}

Output:

c. Write a multithreaded program that generates the Fibonacci sequence. This program should
work as follows: create a class Input that reads the number of Fibonacci numbers that the
program is to generate. The class will then create a separate thread that will generate the
Fibonacci numbers, placing the sequence in an array. When the thread finishes execution,
the parent thread (Input class) will output the sequence generated by the child thread.
Because the parent thread cannot begin outputting the Fibonacci sequence until the child
thread finishes, the parent thread will have to wait for the child thread to finish.

Code: class EXP13c { public static void main(String[] args)


throws InterruptedException { int count = 10;
Object Oriented Programming using Java Laboratory (DJS23FLES201) Academic Year
2024-25

FibonacciGenerator thread = new FibonacciGenerator(count); thread.start();


thread.join();

int[] result = thread.getSequence();


System.out.print("Fibonacci Sequence: ");
for (int num : result) {
System.out.print(num + " ");
}
System.out.println();
}
}

Output:

d. WAP to prevent concurrent booking of a ticket using the concept of thread synchronization.
Code: class TicketSystem
{ int

availableTickets = 3;

synchronized void bookTicket(String name) {


if (availableTickets > 0) {
System.out.println(name + " successfully booked a ticket."); availableTickets--
;

} else {
System.out.println(name + " failed to book. No tickets left.");
}
}
}
Object Oriented Programming using Java Laboratory (DJS23FLES201) Academic Year
2024-25

class BookingThread extends Thread {


TicketSystem system;
String name;

BookingThread(TicketSystem system, String name) {


this.system = system; this.name = name;

public void run() {


system.bookTicket(name);
}
}

class EXP13d { public static void


main(String[] args) { TicketSystem
system = new TicketSystem();

BookingThread t1 = new BookingThread(system, "Alice");


BookingThread t2 = new BookingThread(system, "Bob");
BookingThread t3 = new BookingThread(system, "Cynthia"); BookingThread
t4 = new BookingThread(system, "Daniel");

t1.start(); t2.start(); t3.start(); t4.start();


}

}
Object Oriented Programming using Java Laboratory (DJS23FLES201) Academic Year
2024-25

Output:

e. Write a program to demonstrate thread methods: wait notify suspend resume join setpriority
getpriority setname getname Code: class
EXP13e extends Thread {

public void run() { try {


System.out.println("Running
thread: " + getName());

Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println("Thread interrupted.");
}
}

public static void main(String[] args) throws InterruptedException { EXP13e


thread = new EXP13e(); thread.setName("DemoThread");

System.out.println("Thread name set to: " + thread.getName());


thread.setPriority(Thread.MAX_PRIORITY);
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2024-25

System.out.println("Thread priority set to: " + thread.getPriority());


thread.start(); thread.join();

}
}

Output:

You might also like