Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- === TABLE.JAVA ===
- /**
- * author: Y8142786
- *
- * Disclaimer: this looks hacky because it's what I coded from scratch after my beautiful large, super customizable
- * and very pretty program got a strange bug I could never discover.
- *
- * This program solves the custom Dining Philosophers' Problem by combining two methods, namely
- * the waiter (see the method enoughForks in Table.java) and
- * resource hierarchy (see how I have the same ids for phil and forks and how I treat the last philosopher differently)
- *
- * This guarantees no deadlocks, but also a good uniformity, as I was able to observe myself through repeated tests.
- * if you'd like the comfort of checking out real software, the code is uploaded anonymously on pastebin:
- * Table.java: http://pastebin.com/AW5DaerK
- * Phil.java: http://pastebin.com/mAc91rU2
- * CMonitor.java: http://pastebin.com/nhXC0rzS
- *
- *
- * Everything is pretty standard and self explanatory (maybe except the fact that I have a funnily named function called free()
- * that checks if a monitor is not acquired).
- *
- */
- import java.util.ArrayList;
- public class Table {
- public static ArrayList<Thread> pThreads = new ArrayList<Thread>();
- public static ArrayList<CMonitor> forks = new ArrayList<CMonitor>();
- public static ArrayList<CMonitor> plates = new ArrayList<CMonitor>();
- public Table() {
- }
- // Returns true if there are more than one fork left
- public static Boolean enoughForks() {
- Integer a = 0;
- for(CMonitor mon : Table.forks)
- if(mon.free())
- ++a;
- return a > 1;
- }
- public static void main(String args[]) {
- for(Integer i = 0; i < 5; ++i) {
- Table.pThreads.add(new Thread(new Phil(i)));
- Table.forks.add(new CMonitor());
- }
- for(Integer i = 0; i < 3; ++i) {
- Table.plates.add(new CMonitor());
- }
- for(Integer i = 0; i < 5; ++i)
- Table.pThreads.get(i).start();
- }
- }
- === PHIL.JAVA ===
- public class Phil implements Runnable {
- private Integer mealsEaten, mealsLeft;
- private Integer id;
- public Phil(Integer id) {
- mealsEaten = 0;
- mealsLeft = 10;
- this.id = id;
- }
- public void run() {
- for(; this.mealsEaten < this.mealsLeft; ++this.mealsEaten) {
- try {
- if(id < 4) {
- Table.forks.get(this.id).acquire();
- Table.forks.get(this.id + 1).acquire();
- }
- else {
- Table.forks.get(0).acquire();
- Table.forks.get(this.id).acquire();
- }
- // An integer that keeps track of what empty plate we've found
- Integer plateId = 0;
- // We can't search the same array for a free spot from multiple threads
- synchronized(Table.plates) {
- while(Table.plates.get(plateId).free() == false && plateId < 3)
- ++plateId;
- Table.plates.get(plateId).acquire();
- }
- this.delay("is eating");
- // We ate, we start releasing our stuff, starting with the plates
- // (because we always assume there are plates available)
- Table.plates.get(plateId).release();
- if(id < 4) {
- Table.forks.get(this.id + 1).release();
- Table.forks.get(this.id).release();
- }
- else {
- Table.forks.get(this.id).release();
- Table.forks.get(0).release();
- }
- this.delay("is thinking");
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- public void output(String s) {
- System.out.println("[" + this.id + "] " + s);
- }
- public void delay(String s) throws InterruptedException {
- this.output(s);
- Thread.sleep(200);
- }
- }
- === CMONITOR.JAVA ===
- public class CMonitor {
- private Boolean acquired;
- private Integer value;
- CMonitor() {
- this.acquired = false;
- }
- public synchronized void acquire() throws InterruptedException {
- while(this.acquired == true && Table.enoughForks())
- this.wait();
- this.acquired = false;
- }
- public synchronized void release() {
- this.acquired = false;
- this.notifyAll();
- }
- public Boolean free() {
- return !(this.acquired);
- }
- public Integer getValue() {
- return value;
- }
- public void setValue(Integer value) {
- this.value = value;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment