Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.concurrent.Semaphore;
- public class Demo {
- public static void main(String args[]) throws InterruptedException {
- DiningPhilosophers.runTest();
- }
- }
- class DiningPhilosophers {
- private static Random random = new Random(System.currentTimeMillis());
- private Semaphore[] forks = new Semaphore[5];
- public DiningPhilosophers() {
- forks[0] = new Semaphore(1);
- forks[1] = new Semaphore(1);
- forks[2] = new Semaphore(1);
- forks[3] = new Semaphore(1);
- forks[4] = new Semaphore(1);
- }
- public void lifecycleOfPhilosopher(int id) throws InterruptedException {
- while (true) {
- think(id);
- eat(id);
- }
- }
- void think(int id) throws InterruptedException {
- System.out.println(String.format("%d thinking", id));
- Thread.sleep(random.nextInt(50));
- }
- void eat(int id) throws InterruptedException {
- if (forks[id].tryAcquire()) {
- if (forks[(id - 1 + 5) % 5].tryAcquire()) {
- System.out.println(id + " Eating...");
- }
- forks[(id - 1 + 5) % 5].release();
- }
- forks[id].release();
- // TODO: 3/29/20 Synchronize
- }
- static void runPhilosopher(DiningPhilosophers dp, int id) {
- try {
- dp.lifecycleOfPhilosopher(id);
- } catch (InterruptedException ie) {
- }
- }
- public static void runTest() throws InterruptedException {
- final DiningPhilosophers dp = new DiningPhilosophers();
- Thread p1 = new Thread(new Runnable() {
- public void run() {
- runPhilosopher(dp, 0);
- }
- });
- Thread p2 = new Thread(new Runnable() {
- public void run() {
- runPhilosopher(dp, 1);
- }
- });
- Thread p3 = new Thread(new Runnable() {
- public void run() {
- runPhilosopher(dp, 2);
- }
- });
- Thread p4 = new Thread(new Runnable() {
- public void run() {
- runPhilosopher(dp, 3);
- }
- });
- Thread p5 = new Thread(new Runnable() {
- public void run() {
- runPhilosopher(dp, 4);
- }
- });
- p1.start();
- p2.start();
- p3.start();
- p4.start();
- p5.start();
- p1.join();
- p2.join();
- p3.join();
- p4.join();
- p5.join();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement