Browse Source

Broken multithreaded version (no barriers)

master
Leonora Tindall 2 years ago
parent
commit
823c1f06ad
Signed by: nora GPG Key ID: 99041B68DBC02DAC
  1. 1
      .gitignore
  2. 5
      .idea/vcs.xml
  3. 34
      .idea/workspace.xml
  4. BIN
      out/production/CSCI335Barriers/Main.class
  5. 57
      src/Main.java

1
.gitignore

@ -0,0 +1 @@
*.class

5
.idea/vcs.xml

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
</project>

34
.idea/workspace.xml

@ -1,7 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="3da8c399-6d50-4f60-937d-4daa7aad4637" name="Default Changelist" comment="" />
<list default="true" id="3da8c399-6d50-4f60-937d-4daa7aad4637" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/src/Main.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/Main.java" afterDir="false" />
</list>
<ignored path="$PROJECT_DIR$/out/" />
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" />
@ -18,14 +20,20 @@
<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="450">
<caret line="30" column="20" selection-start-line="30" selection-start-column="20" selection-end-line="30" selection-end-column="20" />
<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" />
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
</leaf>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="IdeDocumentHistory">
<option name="CHANGED_PATHS">
<list>
@ -39,6 +47,7 @@
<option name="width" value="1390" />
<option name="height" value="1001" />
</component>
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectView">
<navigator proportions="" version="1">
<foldersAlwaysOnTop value="true" />
@ -50,6 +59,7 @@
</panes>
</component>
<component name="PropertiesComponent">
<property name="SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
<property name="WebServerToolWindowFactoryState" value="false" />
</component>
<component name="RunDashboard">
@ -91,12 +101,12 @@
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1570414923071</updated>
<workItem from="1570414924458" duration="770000" />
<workItem from="1570414924458" duration="2682000" />
</task>
<servers />
</component>
<component name="TimeTrackingManager">
<option name="totallyTimeSpent" value="770000" />
<option name="totallyTimeSpent" value="2682000" />
</component>
<component name="ToolWindowManager">
<frame x="11" y="-6" width="1898" height="1051" extended-state="6" />
@ -105,15 +115,16 @@
<window_info id="Designer" />
<window_info id="UI Designer" />
<window_info id="Favorites" side_tool="true" />
<window_info active="true" content_ui="combo" id="Project" order="0" visible="true" weight="0.24972972" />
<window_info content_ui="combo" id="Project" order="0" visible="true" weight="0.24972972" />
<window_info id="Structure" order="1" side_tool="true" weight="0.25" />
<window_info anchor="bottom" id="Docker" show_stripe_button="false" />
<window_info anchor="bottom" id="Version Control" />
<window_info anchor="bottom" id="Terminal" />
<window_info anchor="bottom" id="Terminal" weight="0.3290461" />
<window_info anchor="bottom" id="Event Log" side_tool="true" />
<window_info anchor="bottom" id="Messages" weight="0.3290461" />
<window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" />
<window_info anchor="bottom" id="Run" order="2" />
<window_info active="true" anchor="bottom" id="Run" order="2" visible="true" weight="0.3290461" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.4" />
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
@ -132,8 +143,11 @@
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/src/Main.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="450">
<caret line="30" column="20" selection-start-line="30" selection-start-column="20" selection-end-line="30" selection-end-column="20" />
<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" />
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>

BIN
out/production/CSCI335Barriers/Main.class

Binary file not shown.

57
src/Main.java

@ -1,4 +1,8 @@
import java.lang.reflect.Array;
import java.util.Random;
import java.util.concurrent.CyclicBarrier;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;
public class Main {
static int SIZE = 4;
@ -6,6 +10,8 @@ public class Main {
int [][] b;
int [][] c;
int [][] d;
CyclicBarrier barrier;
ArrayList<Integer> workingOn;
public static void main(String[] args) {
var main = new Main();
@ -18,6 +24,10 @@ public class Main {
b = new int[SIZE][SIZE];
c = new int[SIZE][SIZE];
d = new int[SIZE][SIZE];
workingOn = new ArrayList<>();
workingOn.add(0);
workingOn.add(0);
for (int row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++) {
a[row][col] = random.nextInt(10);
@ -31,19 +41,24 @@ public class Main {
printMatrix(a, "A");
printMatrix(b, "B");
for (int row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++) {
c[row][col] = computeAtPosition(a, b, row, col);
synchronized (workingOn) {
workingOn.set(0, workingOn.get(0) + 1);
System.out.println("Start thread " + row);
new Thread(new Worker(row)).start();
}
}
printMatrix(c, "C");
while (workingOn.get(0) + workingOn.get(1) != 0) {
synchronized (workingOn) {
System.out.println("Checking threads." );
try {
Thread.sleep(250);
} catch (InterruptedException e) {
for (int row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++) {
d[row][col] = computeAtPosition(c, a, row, col);
}
}
}
}
}
printMatrix(c, "C");
printMatrix(d, "D");
}
@ -64,4 +79,28 @@ public class Main {
System.out.println();
}
}
class Worker implements Runnable {
int row;
public Worker(int row) { this.row = row; }
public void run() {
for (int col = 0; col < SIZE; col++) {
c[row][col] = computeAtPosition(a, b, row, col);
}
System.out.println("Thread " + row + " done working on C.");
synchronized (workingOn) {
System.out.println("Thread " + row + " marked done working on C.");
workingOn.set(0, workingOn.get(0) - 1);
workingOn.set(1, workingOn.get(1) + 1);
}
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.");
workingOn.set(1, workingOn.get(1) - 1);
}
}
}
}

Loading…
Cancel
Save