Download as:
Rating : ⭐⭐⭐⭐⭐
Price: $10.99
Language:EN
Pages: 7
Words: 873

Problem 2: Threads Synchronisation and Communication

  • FuncA1: A1= $\sum_{i = 0}^{i = 100}i$

  • FuncB1: B1=A1+ $\sum_{i = 0}^{i = 200}i$

  1. Using the mathematical expressions of the functions and their execution dependence as shown in Figure 2.1, give the final correct values of the shared variables1

  • Your java programs

  • A report explaining the implementation of the synchronisation with reference to the relevant lines of your code along with the testing result. Maximum 3 pages.

JAVA

}

}

}

class ThreadA extends Thread {

Thread.yield();

}

}

int B3 = SharedVariables.Bi.get();

class ThreadB extends Thread {

public void run() {

SharedVariables.Ai.set(A2);

while (SharedVariables.step.get() < 2) {

while (SharedVariables.step.get() < 3) {

Thread.yield();

}

}

int iterations = 1000000;

for (int i = 0; i < iterations; i++) {

SharedVariables.step.set(2);

SharedVariables.step.set(3);

e.printStackTrace();

}

System.out.println("Final value of Bi: " + finalBi);

System.out.println("--------------------------------------");

Explanation and Report:

In ThreadA and ThreadB, while loops are introduced to check the value of step before proceeding with certain calculations. These loops will cause the threads to yield until the required step is reached.

The main method now includes a loop to run the threads for a high number of iterations (set by the iterations variable). Before each iteration, step is set to 0 to reset the synchronization.


  1. $\sum_{i = 0}^{n}{i = n*(n + 1)/2}$↩︎

Copyright © 2009-2023 UrgentHomework.com, All right reserved.