aboutsummaryrefslogtreecommitdiff
path: root/CS2452/assign5/src/ResourceReader.java
diff options
context:
space:
mode:
authorTucker Evans <tuckerevans24@gmail.com>2019-02-18 08:10:10 -0500
committerTucker Evans <tuckerevans24@gmail.com>2019-02-18 08:10:10 -0500
commitb4dbd2cfa724476162fa6d35941a5d7cdc9c9524 (patch)
tree431af0b75efa29dfa3bab2868a78ab0eb29173c7 /CS2452/assign5/src/ResourceReader.java
parente8b1808eaf87a49e4c34ebbfb66854baa627418c (diff)
Adds all assignments not previously in a git repo
Diffstat (limited to 'CS2452/assign5/src/ResourceReader.java')
-rwxr-xr-xCS2452/assign5/src/ResourceReader.java89
1 files changed, 89 insertions, 0 deletions
diff --git a/CS2452/assign5/src/ResourceReader.java b/CS2452/assign5/src/ResourceReader.java
new file mode 100755
index 0000000..f9f0f71
--- /dev/null
+++ b/CS2452/assign5/src/ResourceReader.java
@@ -0,0 +1,89 @@
+import java.io.*;
+import java.util.concurrent.Semaphore;
+
+public class ResourceReader implements Runnable
+{
+ public static double cor = 0;
+ public static Semaphore flg1 = new Semaphore(1);
+
+ Thread t;
+ byte[] res = null, sentinel = null;
+ DataOutputStream dout = null;
+ Semaphore flg = null;
+ Semaphore flg2 = null;
+
+ ResourceReader(DataOutputStream dot, byte[] r, Semaphore s, Semaphore s2)
+ {
+ res = r;
+ dout = dot;
+ flg = s;
+ flg2 = s2;
+ sentinel = new byte[32768];
+ for (int i = 0; i < sentinel.length; i++ )
+ sentinel[i] = 65;
+ t = new Thread(this, "ResourceReader");
+ t.start();
+ }
+
+ public void run()
+ {
+ int i;
+ byte value = 33;
+ boolean running = true;
+
+ System.out.println("ResourceReader: starts");
+ while (running)
+ {
+ try
+ {
+
+ flg2.acquire();
+ flg2.release();
+ flg1.acquire();
+ if (cor == 0)
+ {
+ flg.acquire();
+ cor++ ;
+ flg1.release();
+ for (int j = 0; j < 50; j++ )
+ dout.write(res, 0, res.length);
+ dout.write(sentinel, 0, sentinel.length);
+ flg.release();
+ }
+ else
+ {
+ cor++ ;
+ flg1.release();
+ for (int j = 0; j < 50; j++ )
+ dout.write(res, 0, res.length);
+ dout.write(sentinel, 0, sentinel.length);
+ }
+ flg1.acquire();
+ cor-- ;
+ flg1.release();
+
+ System.out.println("ResourceReader: sent a bunch");
+ Thread.sleep(100);
+ running = false;
+ } catch (Exception e)
+ {
+ e.printStackTrace();
+ try
+ {
+ dout.close();
+ } catch (Exception e2)
+ {
+ }
+ return;
+ }
+ }
+ System.out.println("ResourceReader: ends");
+ try
+ {
+ dout.close();
+ } catch (Exception e2)
+ {
+ }
+
+ }
+}