Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

philosophersTask(Bushin) #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 169 additions & 0 deletions src/main/java/lesson_05_04_2018/philosophers/another/Main.java
Original file line number Diff line number Diff line change
@@ -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<Stick> 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!");
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package lesson_05_04_2018.philosophers.another;

public class Stick {
}
39 changes: 39 additions & 0 deletions src/main/java/lesson_05_04_2018/philosophers/another/Waiter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package lesson_05_04_2018.philosophers.another;

import java.util.ArrayList;
import java.util.List;

public class Waiter {
private List<Stick> sticks;
private List<Stick> busySticks;

Waiter(List<Stick> 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);
}
}