GithubHelp home page GithubHelp logo

chizuru-nina-yamauchi / dining-philosophers-problem-concurrency-problem-multi-threading Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 807 KB

My assignment about concurrency problem and synchronization(multi-threading) with Concurrency API (Dinging Philosophers problem) for my online java course.

Java 100.00%
concurrency synchronization thred algorithm

dining-philosophers-problem-concurrency-problem-multi-threading's Introduction

Dining-Philosophers-Problem-Concurrency-Problem-multi-threading

My assignment about concurrency problem and synchronization(multi-threading) with Dinging Philosophers Problem for my online java course. My source code: main -> java My result is at the end in this page.


Assignment: Dining Philosophers with Priority

Background

The Dining Philosophers problem is a classic synchronization problem that demonstrates the challenges of avoiding deadlock while managing limited resources. The problem consists of five philosophers sitting at a round table, where each philosopher thinks and occasionally gets hungry and needs to eat. Between each pair of philosophers, there is a single fork. In order to eat, a philosopher must have both forks, one from their left and one from their right.

Task

Your assignment is to implement a solution to the Dining Philosophers problem using Java's concurrency API, with an added twist: each philosopher has a priority level that determines how urgently they need to eat. Philosophers with higher priority should be given preference when forks are available.

Requirements

  1. Create a class Philosopher that implements Runnable and represents a philosopher with a priority level.
  2. Create a class Fork that represents a fork. Ensure that only one philosopher can hold a fork at a time.
  3. Implement logic in the Philosopher class to pick up both forks, eat for a certain amount of time, and then put down the forks.
  4. Use an ExecutorService with a fixed thread pool to manage the philosopher threads.
  5. Set the priority of each philosopher thread based on their priority level.
  6. Ensure that your implementation avoids deadlock and starvation. Philosophers with higher priority should have a higher chance of eating first, but lower-priority philosophers should not starve.
  7. Print messages to the console when a philosopher is thinking, waiting for forks, eating, and putting down forks.

Sample Output

Philosopher 1 is thinking.
Philosopher 2 is waiting for forks.
Philosopher 3 is eating.
Philosopher 1 is waiting for forks.
Philosopher 3 has finished eating and put down forks.
...

Tips

  • Use synchronized blocks or ReentrantLock to manage access to the forks.
  • Consider using tryLock() with a timeout to avoid deadlock.
  • Use Thread.sleep() to simulate the time spent thinking and eating.
  • You may need to implement additional logic to manage philosopher priorities and ensure fairness.

Additional Challenge

As an additional challenge, implement a "conductor" or "mediator" that controls access to the forks, ensuring that philosophers with higher priority are given preference, but also ensuring that all philosophers eventually get to eat.

This assignment will help students understand how to apply thread priorities in a more complex synchronization scenario. It will also reinforce concepts such as deadlock avoidance, resource management, and fair scheduling.


Result

There was a gap in two philosophers action, and I tried with semaphores as well. But I researched about the problem I was having and decided to accept the issue and went back to use ReentrantLock(since my assignment indicate me to use ReentrantLock or Synchronized). For example, Philosopher 3 picked up the left fork again before Philosopher 4 put down the right fork in the message of the console. But I read this kind of scenario is possible in concurrent programs due to the nature of thread scheduling and the fine-grained timing involved. It doesn't necessarily indicate an error in the program logic. In a concurrent environment, the order of execution between threads can be influenced by factors such as thread priorities, system load, and the specific behavior of JVM. The output might be a result of the slight time difference in the actions of the two philosophers. (Or it might be logically wrong. If you notice some logical issues or mistakes, happy to hear from you.)

pic1

pic2

pic3

pic4

pic5

pic6

pic7

Resources

These resources helped me understand to finish this assignment.

dining-philosophers-problem-concurrency-problem-multi-threading's People

Contributors

chizuru-nina-yamauchi 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.