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?
New code examples in category Java
-
Java 2022-03-27 21:35:04 Sort string array in case insensitive order and case sensitive order java
-
Java 2022-03-27 21:25:10 java -jar -l resources\es.porperties -i ejemplo.txt -o inject.bin
-
Java 2022-03-27 21:20:21 Debug & Fix a 2-Dimensional Array Java Console Application
-
Java 2022-03-27 20:40:19 TreeSet headSet(E toElement) method in java
-
Java 2022-03-27 19:30:06 close keyboard android
-
Java 2022-03-27 19:20:40 java measure execution time
-
Java 2022-03-27 19:10:06 how to add cardview support in android studio
-
Java 2022-03-27 18:55:08 android studio lower case letters on a button