implementation of dining philosopher in java

package com.company;import java.util.concurrent.Semaphore;import java.util.concurrent.ThreadLocalRandom;public class Main {    static int philosopher = 5;    static philosopher philosophers[] = new philosopher[philosopher];    static chopstick chopsticks[] = new chopstick[philosopher];    static class chopstick {        public Semaphore mutex = new Semaphore(1);        void grab() {            try {                mutex.acquire();            }            catch (Exception e) {                e.printStackTrace(System.out);            }        }        void release() {            mutex.release();        }        boolean isFree() {            return mutex.availablePermits() > 0;        }    }    static class philosopher extends Thread {        public int number;        public chopstick leftchopstick;        public chopstick rightchopstick;        philosopher(int num, chopstick left, chopstick right) {            number = num;            leftchopstick = left;            rightchopstick = right;        }        public void run(){            while (true) {                leftchopstick.grab();                System.out.println("philosopher " + (number+1) + " grabs left chopstick.");                rightchopstick.grab();                System.out.println("philosopher " + (number+1) + " grabs right chopstick.");                eat();                leftchopstick.release();                System.out.println("philosopher " + (number+1) + " releases left chopstick.");                rightchopstick.release();                System.out.println("philosopher " + (number+1) + " releases right chopstick.");            }        }        void eat() {            try {                int sleepTime = ThreadLocalRandom.current().nextInt(0, 1000);                System.out.println("philosopher " + (number+1) + " eats for " + sleepTime);                Thread.sleep(sleepTime);            }            catch (Exception e) {                e.printStackTrace(System.out);            }        }    }    public static void main(String argv[]) {        for (int i = 0; i < philosopher; i++) {            chopsticks[i] = new chopstick();        }        for (int i = 0; i < philosopher; i++) {            philosophers[i] = new philosopher(i, chopsticks[i], chopsticks[(i + 1) % philosopher]);            philosophers[i].start();        }        while (true) {            try {                // sleep 1 sec                Thread.sleep(1000);                // check for deadlock                boolean deadlock = true;                for (chopstick f : chopsticks) {                    if (f.isFree()) {                        deadlock = false;                        break;                    }                }                if (deadlock) {                    Thread.sleep(1000);                    System.out.println("Everyone Eats");                    break;                }            }            catch (Exception e) {                e.printStackTrace(System.out);            }        }        System.out.println("Exit The Program!");        System.exit(0);    }}

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source