Advertisement
Guest User

Dekker

a guest
Mar 19th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.45 KB | None | 0 0
  1. public class TestThread extends Thread {
  2.     private A a;
  3.     private int number;
  4.     private static boolean flag1 = false;
  5.     private static boolean flag2 = false;
  6.     private static int turn = 0;
  7.  
  8.     public TestThread(A a, int number) {
  9.         this.a = a;
  10.         this.number = number;
  11.     }
  12.  
  13.     @Override
  14.     public void run() {
  15.         for (int i = 0; i < 100; i++) {
  16.             if (this.number % 2 == 0) {
  17.                 flag1 = true;
  18.                 while (flag2) {
  19.                     if (turn == 1) {
  20.                         flag1 = false;
  21.                         while (turn == 1) {
  22.  
  23.                         }
  24.                         flag1 = true;
  25.                     }
  26.                 }
  27.                 a.setValue(a.getValue() + 1);
  28.                 System.out.println("Thread - " + this.number + ", value = " + a.getValue());
  29.                 turn = 2;
  30.                 flag1 = false;
  31.             } else {
  32.                 flag2 = true;
  33.                 while (flag1) {
  34.                     if (turn == 2) {
  35.                         flag2 = false;
  36.                         while (turn == 2) {
  37.  
  38.                         }
  39.                         flag2 = true;
  40.                     }
  41.                 }
  42.                 a.setValue(a.getValue() + 1);
  43.                 System.out.println("Number: " + this.number + "Value = " + a.getValue());
  44.                 turn = 1;
  45.                 flag2 = false;
  46.             }
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement