ansimionescu

Untitled

Dec 13th, 2012
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.11 KB | None | 0 0
  1. === TABLE.JAVA ===
  2. /**
  3.  * author: Y8142786
  4.  *
  5.  * Disclaimer: this looks hacky because it's what I coded from scratch after my beautiful large, super customizable
  6.  *      and very pretty program got a strange bug I could never discover.
  7.  *
  8.  * This program solves the custom Dining Philosophers' Problem by combining two methods, namely
  9.  *      the waiter (see the method enoughForks in Table.java) and
  10.  *      resource hierarchy (see how I have the same ids for phil and forks and how I treat the last philosopher differently)
  11.  *
  12.  * This guarantees no deadlocks, but also a good uniformity, as I was able to observe myself through repeated tests.
  13.  *      if you'd like the comfort of checking out real software, the code is uploaded anonymously on pastebin:
  14.  *      Table.java:     http://pastebin.com/AW5DaerK
  15.  *      Phil.java:      http://pastebin.com/mAc91rU2
  16.  *      CMonitor.java:  http://pastebin.com/nhXC0rzS
  17.  *     
  18.  *
  19.  * Everything is pretty standard and self explanatory (maybe except the fact that I have a funnily named function called free()
  20.  *      that checks if a monitor is not acquired).
  21.  *
  22.  */
  23.  
  24. import java.util.ArrayList;
  25.  
  26. public class Table {
  27.     public static ArrayList<Thread> pThreads = new ArrayList<Thread>();
  28.     public static ArrayList<CMonitor> forks = new ArrayList<CMonitor>();
  29.     public static ArrayList<CMonitor> plates = new ArrayList<CMonitor>();
  30.    
  31.     public Table() {
  32.     }
  33.    
  34.     // Returns true if there are more than one fork left
  35.     public static Boolean enoughForks() {
  36.         Integer a = 0;
  37.        
  38.         for(CMonitor mon : Table.forks)
  39.             if(mon.free())
  40.                 ++a;
  41.        
  42.         return a > 1;
  43.     }
  44.    
  45.     public static void main(String args[]) {       
  46.        
  47.         for(Integer i = 0; i < 5; ++i) {
  48.             Table.pThreads.add(new Thread(new Phil(i)));
  49.             Table.forks.add(new CMonitor());
  50.         }
  51.        
  52.         for(Integer i = 0; i < 3; ++i) {
  53.             Table.plates.add(new CMonitor());
  54.         }
  55.        
  56.         for(Integer i = 0; i < 5; ++i)
  57.             Table.pThreads.get(i).start();
  58.     }
  59. }
  60.  
  61. === PHIL.JAVA ===
  62.  
  63. public class Phil implements Runnable {
  64.     private Integer mealsEaten, mealsLeft;
  65.     private Integer id;
  66.    
  67.     public Phil(Integer id) {
  68.         mealsEaten = 0;
  69.         mealsLeft = 10;
  70.         this.id = id;
  71.     }
  72.    
  73.     public void run() {
  74.         for(; this.mealsEaten < this.mealsLeft; ++this.mealsEaten) {
  75.             try {
  76.                 if(id < 4) {
  77.                     Table.forks.get(this.id).acquire();
  78.                     Table.forks.get(this.id + 1).acquire();
  79.                 }
  80.                 else {
  81.                     Table.forks.get(0).acquire();
  82.                     Table.forks.get(this.id).acquire();
  83.                 }
  84.                
  85.                 // An integer that keeps track of what empty plate we've found
  86.                 Integer plateId = 0;
  87.                
  88.                 // We can't search the same array for a free spot from multiple threads
  89.                 synchronized(Table.plates) {                   
  90.                     while(Table.plates.get(plateId).free() == false && plateId < 3)
  91.                         ++plateId;
  92.                    
  93.                     Table.plates.get(plateId).acquire();
  94.                 }
  95.                
  96.                 this.delay("is eating");
  97.                
  98.                 // We ate, we start releasing our stuff, starting with the plates
  99.                 // (because we always assume there are plates available)
  100.                 Table.plates.get(plateId).release();
  101.                
  102.                 if(id < 4) {
  103.                     Table.forks.get(this.id + 1).release();
  104.                     Table.forks.get(this.id).release();
  105.                 }
  106.                 else {
  107.                     Table.forks.get(this.id).release();
  108.                     Table.forks.get(0).release();
  109.                 }
  110.                
  111.                 this.delay("is thinking");
  112.                
  113.             } catch (InterruptedException e) {
  114.                 e.printStackTrace();
  115.             }
  116.         }
  117.     }
  118.    
  119.     public void output(String s) {
  120.         System.out.println("[" + this.id + "] " + s);
  121.     }
  122.    
  123.     public void delay(String s) throws InterruptedException {  
  124.         this.output(s);
  125.         Thread.sleep(200);
  126.     }
  127. }
  128.  
  129. === CMONITOR.JAVA ===
  130.  
  131. public class CMonitor {
  132.     private Boolean acquired;
  133.     private Integer value;
  134.    
  135.     CMonitor() {
  136.         this.acquired = false;
  137.     }
  138.    
  139.     public synchronized void acquire() throws InterruptedException {
  140.        
  141.         while(this.acquired == true && Table.enoughForks())
  142.             this.wait();
  143.         this.acquired = false;
  144.     }
  145.    
  146.     public synchronized void release() {
  147.         this.acquired = false;
  148.         this.notifyAll();
  149.     }
  150.  
  151.     public Boolean free() {
  152.         return !(this.acquired);
  153.     }
  154.    
  155.     public Integer getValue() {
  156.         return value;
  157.     }
  158.  
  159.     public void setValue(Integer value) {
  160.         this.value = value;
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment