DEV Community

Partha Sutradhar
Partha Sutradhar

Posted on • Edited on

1 1 1

Multithreading - Dining Philosophers Problem in Java

Introduction

The Dining Philosophers Problem illustrates concurrency challenges, where philosophers seated at a table must pick up chopsticks to eat, leading to potential deadlock.

Solution

  • Chopstick Class: Implements locking mechanisms for chopstick pickup and put down.
public class Chopstick {
    private final Lock lock = new ReentrantLock();
    private final Logger logger = LoggerFactory.getLogger(Chopstick.class);
    public boolean pickUp() {
        try {
            return lock.tryLock(5, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            return false;
        }
    }
    public void pickDown() {
        lock.unlock();
    }
}
Enter fullscreen mode Exit fullscreen mode
  • Philosopher Class: Represents each philosopher, managing chopstick pickup and eating.
public class Philosopher implements Runnable {
    private final Logger logger = LoggerFactory.getLogger(Philosopher.class);
    private int id;
    private Chopstick leftStick;
    private Chopstick rightStick;

    public Philosopher(int id, Chopstick leftStick, Chopstick rightStick) {
        this.id = id;
        this.leftStick = leftStick;
        this.rightStick = rightStick;
    }

    @Override
    public void run() {
        while (true) {
            try {
                logger.info("Philosopher " + id + " is thinking.");
                if (leftStick.pickUp()) {
                    logger.info("Philosopher " + id + " picked up left stick.");
                    if (rightStick.pickUp()) {
                        logger.info("Philosopher " + id + " picked up right stick.");
                        logger.info("Philosopher " + id + " is eating.");
                        rightStick.pickDown();
                        logger.info("Philosopher " + id + " pick down right stick.");
                    }
                    leftStick.pickDown();
                    logger.info("Philosopher " + id + " pick down left stick.");
                }
            } catch (Exception e) {
                Thread.currentThread().interrupt();
                return;
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
  • Main Class (Driver): Sets up philosophers and chopsticks, employing thread pooling for execution.
public class Driver {
    private static final Logger logger = LoggerFactory.getLogger(Driver.class);
    private static final Philosopher[] philosophers = new Philosopher[5];
    private static final Chopstick[] chopsticks = new Chopstick[5];

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(5, runnable -> {
            Thread thread = Executors.defaultThreadFactory().newThread(runnable);
            thread.setPriority(Thread.MAX_PRIORITY);
            return thread;
        });
        for (int i = 0; i < chopsticks.length; i++) {
            chopsticks[i] = new Chopstick();
        }
        for (int i = 0; i < philosophers.length; i++) {
            philosophers[i] = new Philosopher(i + 1, chopsticks[i], chopsticks[(i + 1) % 5]);
            executorService.execute(philosophers[i]);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

By synchronizing chopstick access and implementing a thoughtful eating protocol, we ensure the philosophers can dine peacefully without encountering deadlock or contention issues.

Image of AssemblyAI

Automatic Speech Recognition with AssemblyAI

Experience near-human accuracy, low-latency performance, and advanced Speech AI capabilities with AssemblyAI's Speech-to-Text API. Sign up today and get $50 in API credit. No credit card required.

Try the API

Top comments (0)

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay