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

Richa Java Assignment 9-1

The document contains Java code for a synchronization example involving three threads that print messages using a synchronized method. It includes three classes: Synch, CallMe, and Caller, where Synch initiates the threads, CallMe defines the synchronized method, and Caller represents the individual threads. The output of the program is 'Richa is Best', and a GitHub link is provided for further reference.

Uploaded by

richa.guptanag
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)
16 views3 pages

Richa Java Assignment 9-1

The document contains Java code for a synchronization example involving three threads that print messages using a synchronized method. It includes three classes: Synch, CallMe, and Caller, where Synch initiates the threads, CallMe defines the synchronized method, and Caller represents the individual threads. The output of the program is 'Richa is Best', and a GitHub link is provided for further reference.

Uploaded by

richa.guptanag
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

Java_Assignment_9

Richa Gupta
22070126087
AIML B1
Synch.java

package java_assignment_9;

// Class representing a simple synchronization example


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

// Creating an instance of CallMe, which contains the me


CallMe target = new CallMe();

// Creating three Caller objects, each associated with t


Caller object1 = new Caller(target, "Richa");
Caller object2 = new Caller(target, "is");
Caller object3 = new Caller(target, "best");

// Start the threads associated with each Caller object


object1.t.start();
object2.t.start();
object3.t.start();

try {
// Wait for each thread to finish executing
object1.t.join();
object2.t.join();
object3.t.join();
} catch (InterruptedException e) {
// Handle the case where a thread is interrupted whi

Java_Assignment_9 1
e.printStackTrace();
}
}
}

CallMe.java

package java_assignment_9;

// Class representing a method that prints a message with synchr


public class CallMe {

// Method for printing a message with synchronization


synchronized public void call(String msg) {
// Print the message
System.out.print(msg);
try {
// Simulate some work being done by sleeping for 1 s
Thread.sleep(1000);
} catch (InterruptedException e) {
// Handle the case where the thread is interrupted w
e.printStackTrace();
}
// Add a space after printing the message
System.out.print(" ");
}
}

Caller.java

package java_assignment_9;

// Class representing a caller that invokes a synchronized metho


public class Caller implements Runnable {
Thread t; // Thread associated with this caller
String msg; // Message to be passed to the synchronized meth

Java_Assignment_9 2
CallMe target; // Target object containing the synchronized

// Constructor to initialize the caller with a target object


public Caller(CallMe targ, String s) {
target = targ;
msg = s;
t = new Thread(this);
}

// Method representing the execution logic of the caller


@Override
public void run() {
// Synchronize on the target object to ensure thread saf
synchronized (target) {
// Call the synchronized method of the target object
target.call(msg);
}
}
}

Output: Richa is Best

Github link: https://github.com/RichaGupta1901/threads

Java_Assignment_9 3

You might also like