From 49c6cf6caef7404a4e0114da18d5dcbb4454b62d Mon Sep 17 00:00:00 2001 From: 2210892 <2210892@stud.hs-mannheim.de> Date: Thu, 24 Oct 2024 11:09:54 +0200 Subject: [PATCH] update PU --- eigenes/src/Philosopher.java | 95 ++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 eigenes/src/Philosopher.java diff --git a/eigenes/src/Philosopher.java b/eigenes/src/Philosopher.java new file mode 100644 index 0000000..4c30727 --- /dev/null +++ b/eigenes/src/Philosopher.java @@ -0,0 +1,95 @@ +package pp; + +import java.util.concurrent.locks.Lock; +import java.util.Random; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.ReentrantLock; + +public class Philosopher extends Thread implements IPhilosopher { + + private Philosopher left; + private Philosopher right; + private int seat; + private Lock table; + private Condition tableIsFull; + private final Random random = new Random(); + + + private volatile boolean stopped; + private volatile boolean eating; + + @Override + public void setLeft(IPhilosopher left) { + this.left = (Philosopher) left; + } + + @Override + public void setRight(IPhilosopher right) { + this.right = (Philosopher) right; + } + + @Override + public void setSeat(int seat) { + this.seat = seat; + } + + @Override + public void setTable(Lock table) { + this.table = (ReentrantLock) table; + tableIsFull = table.newCondition(); + + } + + @Override + public void stopPhilosopher() { + this.stopped = true; + interrupt(); + + } + + private void eat() { + table.lock(); + try { + while(this.left.eating || this.right.eating) { + tableIsFull.await(); + } + this.eating = true; + Thread.sleep(this.random.nextInt(PhilosopherExperiment.MAX_TAKING_TIME_MS)); + table.unlock(); + System.out.println("Philosoper at s: " + seat + " is eating"); + Thread.sleep(this.random.nextInt(PhilosopherExperiment.MAX_EATING_DURATION_MS)); + System.out.println("Philosopher at s: " + seat + " has finished eating."); + this.eating = false; + } catch (Exception e) { + this.stopPhilosopher(); + } finally { + if(this.stopped) { + table.unlock(); + } + } + } + private void think(){ + table.lock(); + try { + right.tableIsFull.signal(); + left.tableIsFull.signal(); + table.unlock(); + Thread.sleep(this.random.nextInt(PhilosopherExperiment.MAX_THINKING_DURATION_MS)); + } catch (InterruptedException e) { + this.stopPhilosopher(); + } + } + + @Override + public void run() { + try { + while(!this.stopped) { + think(); + eat(); + } + } catch(Exception e) { + Thread.currentThread().interrupt(); + } + } +} +