KPlanisphere / Pizzeria-Acapulco-Simulation

Proyecto 6 - Programacion Concurrente Y Paralela

Home Page:https://linktr.ee/planisphere.kgz

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Pizzeria Acapulco Simulation

This project simulates the operation of a pizzeria where cooks prepare pizzas and delivery drivers deliver them to customers. It demonstrates the use of Java threads and locks to manage concurrent actions and synchronization between cooks and delivery drivers.

Description

The Pizzeria Acapulco Simulation project uses Java's multithreading capabilities and locking mechanisms to simulate the interactions between cooks and delivery drivers in a pizzeria setting. The main class, PizzeriaAcapulco, sets up the simulation by creating and managing threads for cooks and delivery drivers, ensuring that the events occur in a synchronized and orderly manner.

Features

  • Multithreading: Utilizes Java's threading capabilities to manage concurrent tasks.
  • Synchronization: Employs locks and conditions to coordinate actions between cooks and delivery drivers.
  • Simulation: Creates a realistic scenario of a pizzeria operation using print statements to represent the actions of cooks and delivery drivers.

File Structure

  • pom.xml: Maven configuration file that manages project dependencies and build settings.
  • src/: Contains the source code and test code for the project.
    • main/: Main application code.
      • java/: Java source code directory.
        • com/mycompany/pizzeriaacapulco/: Package directory containing all the Java classes for the project.
          • PizzeriaAcapulco.java: Entry point of the application; sets up and starts the threads for cooks and delivery drivers.
    • test/: Placeholder for test code.
  • target/: Directory generated by Maven during the build process.
    • classes/: Contains compiled bytecode of the Java classes.
      • com/mycompany/pizzeriaacapulco/: Compiled classes for the project.
        • Pizzeria$1.class: Compiled bytecode for an inner class (if any).
        • PizzeriaAcapulco$Cocinero.class: Compiled bytecode for the Cocinero class.
        • PizzeriaAcapulco$Repartidor.class: Compiled bytecode for the Repartidor class.
        • PizzeriaAcapulco.class: Compiled bytecode for the PizzeriaAcapulco class.
    • generated-sources/: Directory for generated sources (if any).
      • annotations/: Placeholder for annotation processors.
    • maven-status/: Directory containing Maven build status files.
      • maven-compiler-plugin/compile/default-compile/: Contains lists of created and input files during compilation.
        • createdFiles.lst: List of files created during the build.
        • inputFiles.lst: List of files used as input during the build.
    • test-classes/: Directory for compiled test classes.

Main Classes and Methods

PizzeriaAcapulco Class

This class is the entry point of the simulation. It creates and manages threads for cooks and delivery drivers, ensuring synchronized interactions.

Main Method:

public static void main(String[] args) {
    Thread[] cocineros = new Thread[2];
    Thread[] repartidores = new Thread[3];

    for (int i = 0; i < cocineros.length; i++) {
        cocineros[i] = new Thread(new Cocinero());
        cocineros[i].start();
    }

    for (int i = 0; i < repartidores.length; i++) {
        repartidores[i] = new Thread(new Repartidor());
        repartidores[i].start();
    }

    // Stop the simulation after a specific time (for demonstration purposes)
    try {
        Thread.sleep(15000); // Simulate running time
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    // Stop the pizzeria operation
    for (Thread cocinero : cocineros) {
        cocinero.interrupt();
    }
    for (Thread repartidor : repartidores) {
        repartidor.interrupt();
    }
}

Cocinero Class

This class represents a cook in the pizzeria. It uses locks and conditions to manage the preparation and placement of pizzas.

Run Method:

public void run() {
    while (true) {
        cocinarUnaPizza();
        lock.lock();
        try {
            while (pizzasEnBandeja == MAX_PIZZAS) {
                System.out.println("Bandeja llena, esperando");
                cocinerosEsperando.await();
            }
            colocarPizza();
            pizzasEnBandeja++;
            repartidoresEsperando.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

Repartidor Class

This class represents a delivery driver in the pizzeria. It uses locks and conditions to manage the retrieval and delivery of pizzas.

Run Method:

public void run() {
    while (true) {
        lock.lock();
        try {
            while (pizzasEnBandeja == 0) {
                System.out.println("Bandeja vacía, esperando");
                repartidoresEsperando.await();
            }
            retirarPizza();
            pizzasEnBandeja--;
            cocinerosEsperando.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
        repartirPizza();
    }
}

Simulated Methods

These methods simulate the actions of cooking, placing, retrieving, and delivering pizzas.

CocinarUnaPizza Method:

static void cocinarUnaPizza() {
    System.out.println("Cocinando una pizza...");
    try {
        Thread.sleep(2000); // Simulate cooking time
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

ColocarPizza Method:

static void colocarPizza() {
    System.out.println("Pizza colocada en la bandeja");
}

RetirarPizza Method:

static void retirarPizza() {
    System.out.println("Pizza retirada de la bandeja");
}

RepartirPizza Method:

static void repartirPizza() {
    System.out.println("Pizza repartida al cliente");
}

Getting Started

To run the simulation, compile the Java files and execute the PizzeriaAcapulco class.

Requirements

  • Java Development Kit (JDK)
  • Maven for managing project dependencies
  • An IDE or text editor for Java development

Usage

  1. Clone the repository.
  2. Navigate to the src/main/java/com/mycompany/pizzeriaacapulco directory.
  3. Compile the Java files using Maven: mvn compile.
  4. Run the simulation using: mvn exec:java -Dexec.mainClass="com.mycompany.pizzeriaacapulco.PizzeriaAcapulco".