-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Dining_Problem.java
61 lines (61 loc) · 1.36 KB
/
Dining_Problem.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.*;
import java.util.concurrent.Semaphore;
public class Dining_Problem
{
public static Semaphore Fork[];
public static int n;
public static void main( String Args[] )
{
Scanner Sc = new Scanner( System.in );
System.out.print("Enter the number of Philosophers : ");
n = Sc.nextInt();
Fork = new Semaphore[n];
for( int i = 0 ; i < n ; i++ )
Fork[i] = new Semaphore(1);
for( int i = 0 ; i < n ; i++ )
new Philosopher("Philosopher " + (i+1) , i);
}
}
class Philosopher implements Runnable
{
int i , l , r;
String Name;
public Philosopher( String Name , int i )
{
this.i = i;
this.Name = Name;
l = i ;
r=((++i)%Dining_Problem.n);
new Thread(this , Name).start();
}
public void run()
{
try
{
Dining_Problem.Fork[l].acquire();
Dining_Problem.Fork[r].acquire();
}
catch( InterruptedException E )
{
System.out.println("Interrupted Exception Caught !!!!! ");
}
finally
{
try
{
System.out.println(Name + " has Started Eating");
Thread.currentThread().sleep(2000);
System.out.println(Name + " has Finished Eating");
}
catch( InterruptedException E )
{
System.out.println("Interrupted Exception is Caught !!!!! ");
}
finally
{
Dining_Problem.Fork[r].release();
Dining_Problem.Fork[l].release();
}
}
}
}