Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package View;
- class Genesis {
- private boolean turnToTalk, agreed;
- private Priest priestOne, priestTwo;
- public static void main(String[] args) {
- Genesis genesis = new Genesis();
- genesis.PriestsTalk();
- }
- private void PriestsTalk() {
- turnToTalk = true;
- Thread0 thread0 = new Thread0();
- thread0.run();
- }
- class Thread0 implements Runnable {
- @Override
- public void run() {
- turnToTalk = false;
- Thread1 thread1 = new Thread1();
- thread1.run();
- while (!turnToTalk) {
- try {
- wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- // after thread woke up
- if (agreed) {
- // do some work
- } else {
- // do some work
- }
- } else {
- PriestsTalk();
- }
- }
- }
- class Thread1 implements Runnable {
- @Override
- public void run() {
- synchronized (this) {
- while (turnToTalk) {
- try {
- wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- Random r = new Random();
- int i = r.nextInt(2);
- String answer =
- i != 0 ? "Yes" : "No";
- if (answer.equals("Yes")) {
- agreed = true;
- turnToTalk = true;
- r = null;
- answer = null;
- notify();
- } else {
- agreed = false;
- turnToTalk = true;
- r = null;
- answer = null;
- notify();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement