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

Message

The document contains code for a multi-threaded flight reservation system that uses ReadWriteLocks to synchronize access to shared flight data. It includes classes for clients, flights, readers that query reservations, and writers that make and cancel reservations. The main method launches reader and writer threads to concurrently access a synchronized flight instance, demonstrating the use of read-write locks to allow multiple reader threads while only one writer can access the data at a time.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Message

The document contains code for a multi-threaded flight reservation system that uses ReadWriteLocks to synchronize access to shared flight data. It includes classes for clients, flights, readers that query reservations, and writers that make and cancel reservations. The main method launches reader and writer threads to concurrently access a synchronized flight instance, demonstrating the use of read-write locks to allow multiple reader threads while only one writer can access the data at a time.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

Client.

java

public class Client {

private static int i;


private final int id;

Client() {
this.id = i++;
}

@Override
public String toString() {
return Integer.toString(this.id);
}

@Override
public int hashCode() {
return id;
}
}

Flight.java

buffer class'ý

public interface Flight {

public Client[] queryReservation() throws InterruptedException;


public void cancelReservation(int seatNo) throws InterruptedException;
public void makeReservation(Client client, int seatNo) throws
InterruptedException;
public int seatCount();
}

Main.java

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

public class Main {

public static void main(String[] args) {

//thread havuzu oluþturulur

ExecutorService application = Executors.newCachedThreadPool();

//thread'ler tanýmlanýr ve çalýþtýrýlýr


Flight flight = new SynchronizedFlight(15);

application.execute(new Reader(flight));

application.execute(new Reader(flight));

application.execute(new Reader(flight));

application.execute(new Reader(flight));

application.execute(new Writer(flight));

application.execute(new Writer(flight));

application.execute(new Writer(flight));

application.execute(new Writer(flight));

application.execute(new Writer(flight));

application.shutdown();

Reader.java

/* = Reader = Consumer */

import java.util.Random;

public class Reader implements Runnable {

private final static Random generator = new Random();


private final Flight flight; //thread'ler tanýmlanýr ve çalýþtýrýlýr

//constructor tanýmlanýr
public Reader(Flight flight) {
this.flight = flight;
}

//thread'in genel run metodu


@Override
public void run() {
for (int count=0; count<this.flight.seatCount()*1.5; count++) {
try {
Thread.sleep(generator.nextInt(3000));
displaySeats(this.flight.queryReservation());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("\n\n\t\tqueryReservation done!\n");
}
// koltuklarýn bilgisinin dizisinin yazdýrýldýðý metot
public static void displaySeats(Client[] arr) {
System.out.printf("\n operation: %16s | seats: ", "queryReservation");
for (int i=0;i<arr.length;i++) {
System.out.printf("[%d|%s]", i, (arr[i] == null) ? "null": arr[i]);
}
System.out.printf("[%s|%s]", "seatNo", "clientNo");
}
}

Synchronizedflight.java

import java.util.concurrent.locks.*;

class SynchronizedFlight implements Flight {

/* Bir ReadWriteLock uygulamasý aþaðýdaki davranýþlarý garanti eder:

Verileri güncelleyen hiçbir iþ parçacýðý olmadýðý sürece birden çok iþ parçacýðý


ayný anda verileri okuyabilir.

Verileri bir defada güncelleyebilen yalnýzca bir iþ parçacýðý yazma kilidi


serbest býrakýlýncaya kadar diðer iþ parçacýklarýna (hem okuyucular hem de
yazarlar) blok yapmaya neden olur.

Bir iþ parçacýðý diðer iþ parçacýklarý okurken verileri güncelleþtirmeye


çalýþýrsa, iþ parçacýðý okuma kilidi serbest býrakýlana kadar da engeller. */

private ReadWriteLock accessLock = new ReentrantReadWriteLock();

private final Lock readLock = accessLock.readLock();

private final Lock writeLock = accessLock.writeLock();

private final Client[] seats; //client tipinde bir oturma yeri dizisi
tanýmlanýr

SynchronizedFlight() {

this(10); //seatsCount olarak 10 döndürür

SynchronizedFlight(int seatsCount) {

this.seats = new Client[seatsCount]; //seats dizisine boyut atanýr


}

//queryReservation() metodu tanýmlanýr.

@Override

public Client[] queryReservation() throws InterruptedException {

readLock.lock();

Client[] s;

try {

s = this.seats;

} finally {

readLock.unlock();

return s;

@Override

public void cancelReservation(int seatNo) throws InterruptedException {

writeLock.lock();

try {

seats[seatNo] = null;

} finally {

writeLock.unlock();

//makeReservation() method

@Override

public void makeReservation(Client client, int seatNo) throws


InterruptedException {
writeLock.lock();

try {if (seats[seatNo]==null) seats[seatNo] = client;

else System.out.println("\nYou can't choose it. You must choose another


seat");

//seats[seatNo] = client

} finally {

writeLock.unlock();

@Override

public int seatCount() {

return this.seats.length;

Writer.java

/* = Writer = Producer */

import java.util.ArrayList;

import java.util.Collections;

import java.util.Random;

public class Writer implements Runnable {

private final static Random generator = new Random();


private final Flight flight;

private ArrayList<Integer> list;

//constructor

public Writer(Flight flight) {

this.flight = flight;

list = new ArrayList<>();

for (int i=0; i<this.flight.seatCount(); i++) {

list.add(i);

Collections.shuffle(list);

@Override

public void run() {

for (int count=0; count<this.flight.seatCount(); count++) {

try {

//makeReservation

Thread.sleep(generator.nextInt(3000));

Client newClient = new Client();

displayState("makeReservation", list.get(count));

this.flight.makeReservation(newClient, list.get(count));

} catch (InterruptedException e) {

e.printStackTrace();

for (int i=0;i<this.flight.seatCount()/2;i++) {


try {

//cancelReservetion

Thread.sleep(generator.nextInt(3000));

displayState("cancelReservation", list.get(i));

this.flight.cancelReservation(list.get(i));

} catch (InterruptedException e) {

e.printStackTrace();

System.out.println("\n\n\t\tcancelReservation and makeReservation done!\


n");

private void displayState(String operation, int seatNo) {

System.out.printf("\n operation: %16s | seatNo: %d ", operation, seatNo);

You might also like