From 823c1f06adca956ed57b78a9fad4d21353ca4720 Mon Sep 17 00:00:00 2001 From: Leonora Tindall Date: Sun, 6 Oct 2019 21:49:29 -0500 Subject: [PATCH] Broken multithreaded version (no barriers) --- .gitignore | 1 + .idea/vcs.xml | 5 +- .idea/workspace.xml | 34 +++++++++---- out/production/CSCI335Barriers/Main.class | Bin 2471 -> 0 bytes src/Main.java | 57 ++++++++++++++++++---- 5 files changed, 75 insertions(+), 22 deletions(-) create mode 100644 .gitignore delete mode 100644 out/production/CSCI335Barriers/Main.class diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6b468b6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.class diff --git a/.idea/vcs.xml b/.idea/vcs.xml index def6a6a..94a25f7 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,7 +1,6 @@ - + - - + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index bf326b1..d611e78 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,7 +1,9 @@ - + + + + + + @@ -50,6 +59,7 @@ + @@ -91,12 +101,12 @@ - @@ -105,15 +115,16 @@ - + - + + - + @@ -132,8 +143,11 @@ - - + + + + + diff --git a/out/production/CSCI335Barriers/Main.class b/out/production/CSCI335Barriers/Main.class deleted file mode 100644 index 1c466470f08e7bf8c62cfdefe7ea68dfc2c584f8..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2471 zcmaJ@O>k3H6#i~rl9!~f5K__tZ9slaQfQ%KQTrop0g0r56hWvWuj!+GGoKNdJb@6#2>n3VI; z7>>noJdSs8B7rHKO!0*Gq;y6OXLU>qgr^E;4+@9^KT|-Po|e)KzY@P$f#|U9*xo3& zvbj^VpKxa_fz?IZv5qz>Ggj@SIa8)4Rdh>c`IK3+9GQwK>{%wAdEVtt(ck;qff3?$yldq~*=Kv-QnhSG3-FSxh$GGAry-he~XvTGgILWp*f? zZB^<7Y_QfL5f|vHmy%9IwC&APs(Ys6kxOY}deNKhQT^1hAX9V&wkZq>wC!AS z7p#Dne$Yc^$#ZL0b$le*?79n$s(?{&9IG}_HtThZCtY8TGKk?<^DyaHw(8q_$DtSD zEYqxTJbAThwJ=|PJ^U^VEzdCsF=3X=Q?_Rf>8J^8e<8nC*aSBIza3c_;tK`7H!;aR zN{|FpGdZYc(ooH0BIhWC_EBWf41PLc27bKY!WY+wjFhRL20J|?5YGs0@`C+1=9jH`_DJTpb) zZ>avpS;8UJU*c%yHcWXW=ur~kr-%n-?|Gu0fzhQa9#nwOj(!`u1j84fqkNmdgEYL! ZIYbB!=LEH7R8b+gce##neTv`de*hzo_V@q* diff --git a/src/Main.java b/src/Main.java index f787320..cc22d30 100644 --- a/src/Main.java +++ b/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 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(); } } + while (workingOn.get(0) + workingOn.get(1) != 0) { + synchronized (workingOn) { + System.out.println("Checking threads." ); + try { + Thread.sleep(250); + } catch (InterruptedException e) { + + } + } + } printMatrix(c, "C"); - - for (int row = 0; row < SIZE; row++) { - for (int col = 0; col < SIZE; col++) { - d[row][col] = computeAtPosition(c, a, row, col); - } - } 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); + } + } + } }