diff --git a/src/main/java/lesson_05_04_2018/philosophers/another/Main.java b/src/main/java/lesson_05_04_2018/philosophers/another/Main.java new file mode 100644 index 0000000..58e9fdf --- /dev/null +++ b/src/main/java/lesson_05_04_2018/philosophers/another/Main.java @@ -0,0 +1,169 @@ +package lesson_05_04_2018.philosophers.another; + +import java.util.Arrays; +import java.util.List; + +public class Main { + public static void main(String[] args) throws InterruptedException { + Stick stick1 = new Stick(); + Stick stick2 = new Stick(); + Stick stick3 = new Stick(); + Stick stick4 = new Stick(); + Stick stick5 = new Stick(); + + List sticks = Arrays.asList(stick1, stick2, stick3, stick4, stick5); + + Waiter waiter = new Waiter(sticks); + + Philosopher philosopher1 = new Philosopher(waiter); + Philosopher philosopher2 = new Philosopher(waiter); + Philosopher philosopher3 = new Philosopher(waiter); + Philosopher philosopher4 = new Philosopher(waiter); + Philosopher philosopher5 = new Philosopher(waiter); + + +// Philosopher philosopher1 = new Philosopher() { +// @Override +// public void eat() { +// synchronized (stick1) { +// takeLeftStick(stick1); +// System.out.println("Philosopher 1 has taken 1 stick"); +// try { +// TimeUnit.MILLISECONDS.sleep(100); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// synchronized (stick2) { +// takeRightStick(stick2); +// System.out.println("Philosopher 1 has taken 2 stick"); +// try { +// TimeUnit.MILLISECONDS.sleep(100); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// releaseSticks(); +// } +// } +// System.out.println("Philosopher 1 ended"); +// } +// }; +// +// Philosopher philosopher2 = new Philosopher() { +// @Override +// public void eat() { +// synchronized (stick2) { +// takeLeftStick(stick2); +// System.out.println("Philosopher 2 has taken 2 stick"); +// try { +// TimeUnit.MILLISECONDS.sleep(100); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// synchronized (stick3) { +// takeRightStick(stick3); +// System.out.println("Philosopher 2 has taken 3 stick"); +// try { +// TimeUnit.MILLISECONDS.sleep(100); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// releaseSticks(); +// } +// } +// System.out.println("Philosopher 2 ended"); +// } +// }; +// +// Philosopher philosopher3 = new Philosopher() { +// @Override +// public void eat() { +// synchronized (stick3) { +// takeLeftStick(stick3); +// System.out.println("Philosopher 3 has taken 3 stick"); +// try { +// TimeUnit.MILLISECONDS.sleep(100); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// synchronized (stick4) { +// takeRightStick(stick4); +// System.out.println("Philosopher 3 has taken 4 stick"); +// try { +// TimeUnit.MILLISECONDS.sleep(100); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// releaseSticks(); +// } +// } +// System.out.println("Philosopher 3 ended"); +// } +// }; +// +// Philosopher philosopher4 = new Philosopher() { +// @Override +// public void eat() { +// synchronized (stick4) { +// takeLeftStick(stick4); +// System.out.println("Philosopher 4 has taken 4 stick"); +// try { +// TimeUnit.MILLISECONDS.sleep(100); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// synchronized (stick5) { +// takeRightStick(stick5); +// System.out.println("Philosopher 4 has taken 5 stick"); +// try { +// TimeUnit.MILLISECONDS.sleep(100); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// releaseSticks(); +// } +// } +// System.out.println("Philosopher 4 ended"); +// } +// }; +// +// Philosopher philosopher5 = new Philosopher() { +// @Override +// public void eat() { +// synchronized (stick5) { +// takeLeftStick(stick5); +// System.out.println("Philosopher 5 has taken 5 stick"); +// try { +// TimeUnit.MILLISECONDS.sleep(100); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// synchronized (stick1) { +// takeRightStick(stick1); +// System.out.println("Philosopher 5 has taken 1 stick"); +// try { +// TimeUnit.MILLISECONDS.sleep(100); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// releaseSticks(); +// } +// } +// System.out.println("Philosopher 5 ended"); +// } +// }; + + philosopher1.start(); + philosopher2.start(); + philosopher3.start(); + philosopher4.start(); + philosopher5.start(); + + philosopher1.join(); + philosopher2.join(); + philosopher3.join(); + philosopher4.join(); + philosopher5.join(); + + System.out.println("Main End!"); + } +} diff --git a/src/main/java/lesson_05_04_2018/philosophers/another/Philosopher.java b/src/main/java/lesson_05_04_2018/philosophers/another/Philosopher.java new file mode 100644 index 0000000..2bd5fd7 --- /dev/null +++ b/src/main/java/lesson_05_04_2018/philosophers/another/Philosopher.java @@ -0,0 +1,61 @@ +package lesson_05_04_2018.philosophers.another; + +import java.util.concurrent.TimeUnit; + +public class Philosopher extends Thread{ + protected Stick leftStick; + protected Stick rightStick; + private Waiter waiter; + + Philosopher(Waiter waiter) { + super("Philosopher"); + this.waiter = waiter; + } + + @Override + public void run() { + try { + eat(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + public boolean takeLeftStick(Stick stick) { + leftStick = stick; + return stick != null; + } + + public boolean takeRightStick(Stick stick) { + rightStick = stick; + return stick != null; + } + + protected void releaseSticks() { + leftStick = null; + rightStick = null; + } + + public void eat() throws InterruptedException { + while (true) { + if (takeLeftStick(waiter.getLeftStick())) + break; + else + TimeUnit.MILLISECONDS.sleep(100); + } + System.out.println("Left stick is taken"); + TimeUnit.SECONDS.sleep(2); + while (true) { + if(takeRightStick(waiter.getRightStick())) { + break; + } + else + TimeUnit.SECONDS.sleep(2); + } + System.out.println("Right stick is taken"); + if(leftStick != null && rightStick != null) { + waiter.releaseSticks(); + TimeUnit.SECONDS.sleep(2); + } + } +} diff --git a/src/main/java/lesson_05_04_2018/philosophers/another/Stick.java b/src/main/java/lesson_05_04_2018/philosophers/another/Stick.java new file mode 100644 index 0000000..3d23e2f --- /dev/null +++ b/src/main/java/lesson_05_04_2018/philosophers/another/Stick.java @@ -0,0 +1,4 @@ +package lesson_05_04_2018.philosophers.another; + +public class Stick { +} diff --git a/src/main/java/lesson_05_04_2018/philosophers/another/Waiter.java b/src/main/java/lesson_05_04_2018/philosophers/another/Waiter.java new file mode 100644 index 0000000..5587c76 --- /dev/null +++ b/src/main/java/lesson_05_04_2018/philosophers/another/Waiter.java @@ -0,0 +1,39 @@ +package lesson_05_04_2018.philosophers.another; + +import java.util.ArrayList; +import java.util.List; + +public class Waiter { + private List sticks; + private List busySticks; + + Waiter(List sticks){ + this.sticks = sticks; + busySticks = new ArrayList<>(); + } + + public synchronized Stick getLeftStick() { + if (busySticks.size() < sticks.size()-1) { + Stick toReturn = sticks.get(busySticks.size()); + busySticks.add(toReturn); + return toReturn; + } + else + return null; + } + + public synchronized Stick getRightStick() { + if (busySticks.size() < sticks.size()) { + Stick toReturn = sticks.get(busySticks.size()); + busySticks.add(toReturn); + return toReturn; + } + else + return null; + } + + public synchronized void releaseSticks() { + busySticks.remove(busySticks.size()-1); + busySticks.remove(busySticks.size()-1); + } +}