1 20 36 class Property { 37 static boolean flag = true; 38 } 39 40 43 class Process1 extends Thread { 44 public int y1 = 0; 45 private Process2 p2; 46 47 public void run () { 48 while (true) { 49 y1 = p2.y2 + 1; 50 51 while ((p2.y2 != 0) && (y1 >= p2.y2)) { 53 ; 54 } 55 56 57 Property.flag = false; 59 Property.flag = true; 60 61 y1 = 0; 62 } 63 } 64 65 void SetThread (Process2 p) { 66 p2 = p; 67 } 68 } 69 70 73 class Process2 extends Thread { 74 public int y2 = 0; 75 private Process1 p1; 76 77 public void run () { 78 while (true) { 79 y2 = p1.y1 + 1; 80 81 while ((p1.y1 != 0) && (y2 > p1.y1)) { 83 ; 84 } 85 86 87 Property.flag = false; 89 Property.flag = true; 90 91 y2 = 0; 92 } 93 } 94 95 void SetThread (Process1 p) { 96 p1 = p; 97 } 98 } 99 100 103 class Bakery_original { 104 public static void main (String [] args) { 105 Process1 process1 = new Process1(); 106 Process2 process2 = new Process2(); 107 process1.SetThread(process2); 108 process2.SetThread(process1); 109 110 process1.start(); 111 process2.start(); 112 } 113 } | Popular Tags |