dinning phillosoper

 import java.util.concurrent.*;


class DiningPhilosophers {

    static final int NUM_PHILOSOPHERS = 5;
   
    // Forks are shared resources, each philosopher will access them.
    static Semaphore[] forks = new Semaphore[NUM_PHILOSOPHERS];
   
    // Philosopher threads
    static Philosopher[] philosophers = new Philosopher[NUM_PHILOSOPHERS];

    public static void main(String[] args) {
        // Initialize the forks (semaphores)
        for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
            forks[i] = new Semaphore(1); // 1 means each fork is initially available
        }

        // Create philosopher threads
        for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
            philosophers[i] = new Philosopher(i);
            new Thread(philosophers[i]).start();
        }
    }

    // Philosopher class
    static class Philosopher implements Runnable {
        private final int id;

        public Philosopher(int id) {
            this.id = id;
        }

        @Override
        public void run() {
            while (true) {
                think();
                eat();
            }
        }

        private void think() {
            try {
                // Philosopher is thinking
                System.out.println("Philosopher " + id + " is thinking.");
                Thread.sleep((int) (Math.random() * 1000)); // Thinking for a while
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }

        private void eat() {
            try {
                // Pick up forks
                pickUpForks();

                // Philosopher is eating
                System.out.println("Philosopher " + id + " is eating.");
                Thread.sleep((int) (Math.random() * 1000)); // Eating for a while

                // Put down forks
                putDownForks();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }

        private void pickUpForks() throws InterruptedException {
            int leftFork = id;
            int rightFork = (id + 1) % NUM_PHILOSOPHERS;

            // To prevent deadlock, philosopher always picks up the lower-numbered fork first
            if (leftFork < rightFork) {
                forks[leftFork].acquire();
                System.out.println("Philosopher " + id + " picked up left fork " + leftFork);
                forks[rightFork].acquire();
                System.out.println("Philosopher " + id + " picked up right fork " + rightFork);
            } else {
                forks[rightFork].acquire();
                System.out.println("Philosopher " + id + " picked up right fork " + rightFork);
                forks[leftFork].acquire();
                System.out.println("Philosopher " + id + " picked up left fork " + leftFork);
            }
        }

        private void putDownForks() {
            int leftFork = id;
            int rightFork = (id + 1) % NUM_PHILOSOPHERS;

            // Put down forks
            forks[leftFork].release();
            System.out.println("Philosopher " + id + " put down left fork " + leftFork);
            forks[rightFork].release();
            System.out.println("Philosopher " + id + " put down right fork " + rightFork);
        }
    }
}

Comments

Popular posts from this blog

q3part b

Literal table

quicksort 1