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

Synchronization

The document explains three methods of synchronization in Java: synchronized methods, synchronized blocks, and static synchronization. Each method ensures that only one thread can access a particular section of code at a time, preventing conflicts when multiple threads attempt to book seats in a PVR system. Examples of each synchronization method are provided through code snippets demonstrating their implementation in a booking application.

Uploaded by

xewix60590
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)
4 views3 pages

Synchronization

The document explains three methods of synchronization in Java: synchronized methods, synchronized blocks, and static synchronization. Each method ensures that only one thread can access a particular section of code at a time, preventing conflicts when multiple threads attempt to book seats in a PVR system. Examples of each synchronization method are provided through code snippets demonstrating their implementation in a booking application.

Uploaded by

xewix60590
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

Synchronization

1) Synchronized Method (Object Level Lock)

Only one thread can access the method at a time using object-level lock.
class PVR {
int total_seats = 10;

synchronized void bookSeat(int seats) {


if (total_seats >= seats) {
[Link]([Link]().getName() + " booked successfully");
total_seats = total_seats - seats;
[Link]("Seats left = " + total_seats);
} else {
[Link]([Link]().getName() + " booking failed");
[Link]("Seats left = " + total_seats);
}
}
}

class BookingApp extends Thread {


static PVR p;
int seats;

public void run() {


[Link](seats);
}

public static void main(String[] args) {


p = new PVR();

BookingApp bookMyShow = new BookingApp();


BookingApp district = new BookingApp();

[Link]("BookMyShow");
[Link]("District");

[Link] = 7;
[Link] = 6;

[Link]();
[Link]();
}
}
2) Synchronized Block

Only a specific part of the code is locked using object-level lock.


class PVR {
int total_seats = 10;

void bookSeat(int seats) {


synchronized (this) {
if (total_seats >= seats) {
[Link]([Link]().getName() + " booked successfully");
total_seats = total_seats - seats;
[Link]("Seats left = " + total_seats);
} else {
[Link]([Link]().getName() + " booking failed");
[Link]("Seats left = " + total_seats);
}
}
}
}

class BookingApp extends Thread {


static PVR p;
int seats;

public void run() {


[Link](seats);
}

public static void main(String[] args) {


p = new PVR();

BookingApp bookMyShow = new BookingApp();


BookingApp district = new BookingApp();

[Link]("BookMyShow");
[Link]("District");

[Link] = 7;
[Link] = 6;

[Link]();
[Link]();
}
}
3) Static Synchronization (Class Level Lock)

Lock is applied on class level, shared by all threads.


class PVR {
static int total_seats = 20;

static synchronized void bookSeat(int seats) {


if (total_seats >= seats) {
[Link]([Link]().getName() + " booked successfully");
total_seats = total_seats - seats;
[Link]("Seats left = " + total_seats);
} else {
[Link]([Link]().getName() + " booking failed");
[Link]("Seats left = " + total_seats);
}
}
}

class BookMyShow extends Thread {


int seats;

BookMyShow(int seats) {
[Link] = seats;
}

public void run() {


[Link](seats);
}
}

class District extends Thread {


int seats;

District(int seats) {
[Link] = seats;
}

public void run() {


[Link](seats);
}
}

class PVRApp extends Thread {


int seats;

PVRApp(int seats) {
[Link] = seats;
}

public void run() {


[Link](seats);
}
}

public class Main {


public static void main(String[] args) {
BookMyShow t1 = new BookMyShow(7);
District t2 = new District(6);
PVRApp t3 = new PVRApp(5);

[Link]("BookMyShow");
[Link]("District");
[Link]("PVRApp");

[Link]();
[Link]();
[Link]();
}
}

You might also like