|
| 1 | +package javacodes; |
| 2 | + |
| 3 | + |
| 4 | +public class Dine{ |
| 5 | + |
| 6 | + public static void main(String[] args){ |
| 7 | + |
| 8 | + int x=10; |
| 9 | + |
| 10 | + Log.msg(String.valueOf(x)); |
| 11 | + |
| 12 | + Chopstick[] chopistics = new Chopstick[5]; |
| 13 | + |
| 14 | + for(int i=0; i< chopistics.length; i++){ |
| 15 | + |
| 16 | + chopistics[i] = new Chopstick("C: "+i); |
| 17 | + } |
| 18 | + |
| 19 | + Philosopher[] philosophers = new Philosopher[5]; |
| 20 | + |
| 21 | + philosophers[0] = new Philosopher("P: 0 - ", chopistics[0], chopistics[1]); |
| 22 | + |
| 23 | + philosophers[1] = new Philosopher("P: 1 - ", chopistics[1], chopistics[2]); |
| 24 | + |
| 25 | + philosophers[2] = new Philosopher("P: 2 - ", chopistics[2], chopistics[3]); |
| 26 | + |
| 27 | + philosophers[3] = new Philosopher("P: 3 - ", chopistics[3], chopistics[4]); |
| 28 | + |
| 29 | + philosophers[4] = new Philosopher("P: 4 - ", chopistics[4], chopistics[0]); |
| 30 | + |
| 31 | + for(int i=0;i<philosophers.length;i++){ |
| 32 | + |
| 33 | + Log.msg("Thred "+ i); |
| 34 | + |
| 35 | + Thread t= new Thread( philosophers[i]); |
| 36 | + |
| 37 | + t.start(); |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | + |
| 43 | +class Philosopher extends Thread |
| 44 | +{ |
| 45 | + private Chopstick leftChopistick; |
| 46 | + |
| 47 | + private Chopstick rightChopistick; |
| 48 | + |
| 49 | + private String name; |
| 50 | + |
| 51 | + private int state; |
| 52 | + |
| 53 | + public Philosopher ( String name, Chopstick left, Chopstick right){ |
| 54 | + |
| 55 | + this.state = 1; |
| 56 | + |
| 57 | + this.name = name; |
| 58 | + |
| 59 | + leftChopistick = left; |
| 60 | + |
| 61 | + rightChopistick = right; |
| 62 | + } |
| 63 | + |
| 64 | + public void eat() |
| 65 | + { |
| 66 | + if(! leftChopistick.used){ |
| 67 | + |
| 68 | + if(!rightChopistick.used){ |
| 69 | + |
| 70 | + leftChopistick.take(); |
| 71 | + |
| 72 | + rightChopistick.take(); |
| 73 | + |
| 74 | + Log.msg(name + " : Eat"); |
| 75 | + |
| 76 | + Log.Delay(1000); |
| 77 | + |
| 78 | + leftChopistick.release(); |
| 79 | + |
| 80 | + rightChopistick.release(); |
| 81 | + } |
| 82 | + } |
| 83 | + think(); |
| 84 | + } |
| 85 | + |
| 86 | + public void think(){ |
| 87 | + |
| 88 | + this.state = 1; |
| 89 | + |
| 90 | + Log.msg(name + " : Think"); |
| 91 | + |
| 92 | + Log.Delay(1000); |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + public void run(){ |
| 97 | + |
| 98 | + for(int i=0; i<=10; i++){ |
| 99 | + |
| 100 | + eat(); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +class Log{ |
| 106 | + |
| 107 | + public static void msg(String msg){ |
| 108 | + |
| 109 | + System.out.println(msg); |
| 110 | + } |
| 111 | + public static void Delay(int ms){ |
| 112 | + try{ |
| 113 | + Thread.sleep(ms); |
| 114 | + } |
| 115 | + catch(InterruptedException ex){ } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +class Chopstick{ |
| 120 | + |
| 121 | + public boolean used; |
| 122 | + |
| 123 | + public String name; |
| 124 | + |
| 125 | + public Chopstick(String name){ |
| 126 | + |
| 127 | + this.name = name; |
| 128 | + } |
| 129 | + |
| 130 | + public synchronized void take() { |
| 131 | + |
| 132 | + Log.msg ("Used :: " + name ); |
| 133 | + |
| 134 | + this.used = true; |
| 135 | + } |
| 136 | + public synchronized void release() { |
| 137 | + |
| 138 | + Log.msg ("Released :: " + name ); |
| 139 | + |
| 140 | + this.used = false ; |
| 141 | + } |
| 142 | +} |
0 commit comments