Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TestThread extends Thread {
- private A a;
- private int number;
- private static boolean flag1 = false;
- private static boolean flag2 = false;
- private static int turn = 0;
- public TestThread(A a, int number) {
- this.a = a;
- this.number = number;
- }
- @Override
- public void run() {
- for (int i = 0; i < 100; i++) {
- if (this.number % 2 == 0) {
- flag1 = true;
- while (flag2) {
- if (turn == 1) {
- flag1 = false;
- while (turn == 1) {
- }
- flag1 = true;
- }
- }
- a.setValue(a.getValue() + 1);
- System.out.println("Thread - " + this.number + ", value = " + a.getValue());
- turn = 2;
- flag1 = false;
- } else {
- flag2 = true;
- while (flag1) {
- if (turn == 2) {
- flag2 = false;
- while (turn == 2) {
- }
- flag2 = true;
- }
- }
- a.setValue(a.getValue() + 1);
- System.out.println("Number: " + this.number + "Value = " + a.getValue());
- turn = 1;
- flag2 = false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement