0% found this document useful (0 votes)
17 views11 pages

Expt 10B

The document outlines two Java programming experiments focusing on multi-threading. The first program prints 'Welcome to SPIT' and 'Electronics Engineering Department' using threads with synchronization methods, while the second program generates a random integer, computes its square and cube using three threads. The conclusion highlights the learning of wait and notify methods in thread synchronization.

Uploaded by

nachiket.kale24
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)
17 views11 pages

Expt 10B

The document outlines two Java programming experiments focusing on multi-threading. The first program prints 'Welcome to SPIT' and 'Electronics Engineering Department' using threads with synchronization methods, while the second program generates a random integer, computes its square and cube using three threads. The conclusion highlights the learning of wait and notify methods in thread synchronization.

Uploaded by

nachiket.kale24
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/ 11

Name: Nachiket Kale

UID: 2024800052

Experiment No. 10B

AIM:

Program 1

PROBLEM Write a program to print "Welcome to SPIT" and "Electronics


STATEMENT : Engineering Department" continuously on the screen in Java using
threads. Add a sleep method in the welcome thread to delay its
execution for 200ms.

(Use wait(), notify() )

Welcome to SPIT Electronics Engineering Department!

Welcome to SPIT Electronics Engineering Department!

Welcome to SPIT Electronics Engineering Department!

PROGRAM: class SharedResource {


boolean flag = false;

synchronized void printWelcome() {


try {
while (!flag) {
wait();
}
System.out.println("Welcome to Spit");
Thread.sleep(200);
flag = false;
notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

synchronized void printDepartment() {


try {
while (flag) {
wait();
}
System.out.println("Nachiket's Electronics
Department");
Thread.sleep(200);
flag = true;
notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

synchronized void startProcess() {


flag = true;
notify();
}
}
class WelcomeThread extends Thread {
SharedResource resource;

WelcomeThread(SharedResource r) {
resource = r;
}

public void run() {


while (true) {
resource.printWelcome();
}
}
}

class DepartmentThread extends Thread {


SharedResource resource;

DepartmentThread(SharedResource r) {
resource = r;
}

public void run() {


while (true) {
resource.printDepartment();
}
}
}

public class Main {


public static void main(String[] args) {
SharedResource resource = new SharedResource();
WelcomeThread t1 = new WelcomeThread(resource);
DepartmentThread t2 = new
DepartmentThread(resource);

t1.start();
t2.start();

resource.startProcess();
}
}

RESULT:

PROGRAM 2

PROBLEM Write a Java program that implements a multi-thread application that


STATEMENT:
has three threads. First thread generates a random integer for every 1
second; second thread computes the square of the number and prints;
third thread will print the value of cube of the number.

Output:

Random Integer generated : 82

Square of 82 = 6724

Cube of 82 = 551368

Random Integer generated : 100

Square of 100 = 10000

Cube of 100 = 1000000

PROGRAM:
class RandomGenerator extends Thread {
int num;

public void run() {


while (true) {
num = (int)(Math.random() * 100) + 1;
System.out.println("Random" + num);
try {
Thread.sleep(1000);
} catch (Exception e) {
System.out.println("Error in RandomGenerator: " + e);
}
}
}

public int getNum() {


return num;
}
}
class SquarePrinter extends Thread {
RandomGenerator nachiRef;

SquarePrinter(RandomGenerator nachiRef) {
this.nachiRef = nachiRef;
}

public void run() {


while (true) {
int x = nachiRef.getNum();
System.out.println("Square of " + x + " = " + (x * x));
try {
Thread.sleep(1000);
} catch (Exception e) {
System.out.println("Error: " + e);
}
}
}
}

class CubePrinter extends Thread {


RandomGenerator nachiRef;

CubePrinter(RandomGenerator nachiRef) {
this.nachiRef = nachiRef;
}

public void run() {


while (true) {
int x = nachiRef.getNum();
System.out.println("Cube of " + x + " = " + (x * x * x));
try {
Thread.sleep(1000);
} catch (Exception e) {
System.out.println("Error: " + e);
}
}
}
}
public class Threading {
public static void main(String[] args) {
RandomGenerator nachi = new RandomGenerator();
SquarePrinter squareThread = new SquarePrinter(nachi);
CubePrinter cubeThread = new CubePrinter(nachi);

nachi.start();
squareThread.start();
cubeThread.start();
}
}
RESULT:
CONCLUSION:
In this experiment i learned how to wait and notify. i saw how the notify
waits and then synchronises with other thread. Firstly its printed spit and
then nachiket’s electronic branch and then in 2nd question we saw how the
random function generating random number generates squares and cube.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy