Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. package View;
  2.  
  3. class Genesis {
  4.  
  5. private boolean turnToTalk, agreed;
  6. private Priest priestOne, priestTwo;
  7.  
  8. public static void main(String[] args) {
  9. Genesis genesis = new Genesis();
  10. genesis.PriestsTalk();
  11. }
  12.  
  13. private void PriestsTalk() {
  14. turnToTalk = true;
  15. Thread0 thread0 = new Thread0();
  16. thread0.run();
  17. }
  18.  
  19. class Thread0 implements Runnable {
  20. @Override
  21. public void run() {
  22. turnToTalk = false;
  23.  
  24. Thread1 thread1 = new Thread1();
  25. thread1.run();
  26.  
  27. while (!turnToTalk) {
  28. try {
  29. wait();
  30. } catch (InterruptedException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34.  
  35. // after thread woke up
  36. if (agreed) {
  37. // do some work
  38. } else {
  39. // do some work
  40. }
  41. } else {
  42. PriestsTalk();
  43. }
  44. }
  45. }
  46.  
  47. class Thread1 implements Runnable {
  48. @Override
  49. public void run() {
  50. synchronized (this) {
  51. while (turnToTalk) {
  52. try {
  53. wait();
  54. } catch (InterruptedException e) {
  55. e.printStackTrace();
  56. }
  57. }
  58.  
  59. Random r = new Random();
  60. int i = r.nextInt(2);
  61. String answer =
  62. i != 0 ? "Yes" : "No";
  63.  
  64. if (answer.equals("Yes")) {
  65. agreed = true;
  66. turnToTalk = true;
  67. r = null;
  68. answer = null;
  69. notify();
  70. } else {
  71. agreed = false;
  72. turnToTalk = true;
  73. r = null;
  74. answer = null;
  75. notify();
  76. }
  77. }
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement