Working concurrent version (with barrier)

This commit is contained in:
Leonora Tindall 2019-10-06 22:12:48 -05:00
parent 823c1f06ad
commit f1be1b1f32
Signed by: nora
GPG Key ID: 99041B68DBC02DAC
2 changed files with 24 additions and 16 deletions

View File

@ -20,8 +20,8 @@
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/src/Main.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="420">
<caret line="52" column="24" selection-start-line="52" selection-start-column="24" selection-end-line="52" selection-end-column="24" />
<state relative-caret-position="259">
<caret line="54" selection-start-line="54" selection-end-line="54" />
<folding>
<element signature="imports" expanded="true" />
</folding>
@ -101,12 +101,12 @@
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1570414923071</updated>
<workItem from="1570414924458" duration="2682000" />
<workItem from="1570414924458" duration="2937000" />
</task>
<servers />
</component>
<component name="TimeTrackingManager">
<option name="totallyTimeSpent" value="2682000" />
<option name="totallyTimeSpent" value="2937000" />
</component>
<component name="ToolWindowManager">
<frame x="11" y="-6" width="1898" height="1051" extended-state="6" />
@ -143,8 +143,8 @@
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/src/Main.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="420">
<caret line="52" column="24" selection-start-line="52" selection-start-column="24" selection-end-line="52" selection-end-column="24" />
<state relative-caret-position="259">
<caret line="54" selection-start-line="54" selection-end-line="54" />
<folding>
<element signature="imports" expanded="true" />
</folding>

View File

@ -1,5 +1,6 @@
import java.lang.reflect.Array;
import java.util.Random;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;
@ -24,6 +25,7 @@ public class Main {
b = new int[SIZE][SIZE];
c = new int[SIZE][SIZE];
d = new int[SIZE][SIZE];
barrier = new CyclicBarrier(SIZE);
workingOn = new ArrayList<>();
workingOn.add(0);
workingOn.add(0);
@ -50,12 +52,7 @@ public class Main {
}
while (workingOn.get(0) + workingOn.get(1) != 0) {
synchronized (workingOn) {
System.out.println("Checking threads." );
try {
Thread.sleep(250);
} catch (InterruptedException e) {
}
Thread.yield();
}
}
printMatrix(c, "C");
@ -86,19 +83,30 @@ public class Main {
public void run() {
for (int col = 0; col < SIZE; col++) {
c[row][col] = computeAtPosition(a, b, row, col);
Thread.yield();
}
System.out.println("Thread " + row + " done working on C.");
synchronized (workingOn) {
System.out.println("Thread " + row + " marked done working on C.");
System.out.println("Thread " + row + " done working on C.");
workingOn.set(0, workingOn.get(0) - 1);
workingOn.set(1, workingOn.get(1) + 1);
}
try {
System.out.println("Thread " + row + " waiting on barrier.");
barrier.await();
System.out.println("Thread " + row + " done waiting on barrier.");
} catch (BrokenBarrierException e) {
System.out.println("Barrier broken!");
return;
} catch (InterruptedException e) {
System.out.println("Thread " + row + " interrupted!");
return;
}
for (int col = 0; col < SIZE; col++) {
d[row][col] = computeAtPosition(c, a, row, col);
}
System.out.println("Thread " + row + " done working on D.");
synchronized (workingOn) {
System.out.println("Thread " + row + " marked done working on D.");
System.out.println("Thread " + row + " done working on D.");
workingOn.set(1, workingOn.get(1) - 1);
}
}