Git Product home page Git Product logo

cop4520-assignment-2's Introduction

Assignment 2

Problem 1

Strategy

This problem is similar to the prisoner and light switch problem that we had encountered in class. Here are the steps I followed to solve the problem:

  • Assign any one of the guests to be the counter. The counter will keep track of the number of guests to pass through the labyrinth. In my case, I assigned the counter to be the first guest.
  • The counter is initialized to 0 to account for himself.

When the counter enters the room...

  • If there is no cupcake, then the counter will increment by one because that means someone has passed through and eaten the cupcake.
  • If there is a cupcake, then the counter will not increment because that means the cupcake has not been eaten yet.
  • When the count reaches the number of guests, then the counter will announce that all the guests have passed through the labyrinth.

When any other guest enters the room...

  • If there is no cupcake, then the guest will do nothing.
  • If there is a cupcake, then the guest will eat the cupcake only if it is their first time entering the room with the cupcake present. This is to ensure that the cupcake is not eaten more than once by a guest.

In this simulation, I used a Reentrant lock to ensure that only one guest can enter the labyrinth at a time. I also toggled on the fairness policy so that the "Guest" threads will not hog the lock and preveneting the "Counter" guest thread from entering the labyrinth or other guests from eating the cupcake.

How to run

cd "Problem 1"
javac Minotaur.java Guest.java 
java Minotaur

You can change the number of guests by altering the value of the NUM_GUESTS variable in the Minotaur.java file.

You can also change the speed of the simulation by altering the value of the TIME_MULTIPLIER variable in the Minotaur.java file. This is multiplied to every Thread.sleep() call in the program. It is currently set to 0 so that the simulation does not sleep at all.

Problem 2

Strategy

For this problem, I chose to implement soluton #2 from the assignment description.

I believe this strategy is most fair because it ensures that all guests who join the line will eventually get a turn to observe the vase.

To simulate a queue, I used a Reentrant lock to ensure that only one guest can enter the showroom at a time. I also toggled on the fairness policy so that the "Guest" threads that wait the longest will be granted access to the showroom first.

How to run

cd "Problem 2"
javac Party.java Guest.java
java Party

You can change the number of guests by altering the value of the NUM_GUESTS variable in the Party.java file.

You can also change the duration of the party by altering the value of the PARTY_DURATION_SECONDS variable in the Party.java file.

You can also change the speed of the simulation by altering the value of the TIME_MULTIPLIER variable in the Party.java file. This is multiplied to every Thread.sleep() call in the program.

cop4520-assignment-2's People

Contributors

mattsingh avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.