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

Read Write Problem Program

This Java code implements the reader-writer problem using semaphores. It creates Read and Write classes that implement the reader and writer threads. The readLock semaphore is used to control access to shared data between multiple readers, while the writeLock semaphore prevents reads during writes. Main creates sample reader and writer threads to demonstrate the locking behavior.

Uploaded by

naveen.aptra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Read Write Problem Program

This Java code implements the reader-writer problem using semaphores. It creates Read and Write classes that implement the reader and writer threads. The readLock semaphore is used to control access to shared data between multiple readers, while the writeLock semaphore prevents reads during writes. Main creates sample reader and writer threads to demonstrate the locking behavior.

Uploaded by

naveen.aptra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.util.concurrent.

Semaphore;

class ReaderWritersProblem {

static Semaphore readLock = new Semaphore(1);

static Semaphore writeLock = new Semaphore(1);

static int readCount = 0;

static class Read implements Runnable {

@Override

public void run() {

try {

//Acquire Section

readLock.acquire();

readCount++;

if (readCount == 1) {

writeLock.acquire();

readLock.release();

//Reading section

System.out.println("Thread "+Thread.currentThread().getName() + " is READING");

Thread.sleep(1500);

System.out.println("Thread "+Thread.currentThread().getName() + " has FINISHED


READING");

//Releasing section

readLock.acquire();

readCount--;

if(readCount == 0) {

writeLock.release();
}

readLock.release();

} catch (InterruptedException e) {

System.out.println(e.getMessage());

static class Write implements Runnable {

@Override

public void run() {

try {

writeLock.acquire();

System.out.println("Thread "+Thread.currentThread().getName() + " is WRITING");

Thread.sleep(2500);

System.out.println("Thread "+Thread.currentThread().getName() + " has finished


WRITING");

writeLock.release();

} catch (InterruptedException e) {

System.out.println(e.getMessage());

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

Read read = new Read();

Write write = new Write();

Thread t1 = new Thread(read);

t1.setName("thread1");

Thread t2 = new Thread(read);

t2.setName("thread2");
Thread t3 = new Thread(write);

t3.setName("thread3");

Thread t4 = new Thread(read);

t4.setName("thread4");

t1.start();

t3.start();

t2.start();

t4.start();

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