summaryrefslogtreecommitdiff
path: root/Year_3/TSDWL/ex_20131219/Thread2.java
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-12-01 23:48:05 +0100
committerSanto Cariotti <santo@dcariotti.me>2021-12-01 23:48:05 +0100
commit4bcd3b16da0eb0f0efe4fb5eb1e8c6bea9e79cf2 (patch)
tree6807da54f1f8b7a3f6f669e412adb20e9c9e2a58 /Year_3/TSDWL/ex_20131219/Thread2.java
parent26d58225f0cb3256bf1c715257d2882271f3ecae (diff)
tsdwl: adds in java
Diffstat (limited to 'Year_3/TSDWL/ex_20131219/Thread2.java')
-rw-r--r--Year_3/TSDWL/ex_20131219/Thread2.java31
1 files changed, 31 insertions, 0 deletions
diff --git a/Year_3/TSDWL/ex_20131219/Thread2.java b/Year_3/TSDWL/ex_20131219/Thread2.java
new file mode 100644
index 0000000..152e9e0
--- /dev/null
+++ b/Year_3/TSDWL/ex_20131219/Thread2.java
@@ -0,0 +1,31 @@
+public class Thread2 extends Thread {
+ private Shared mem;
+
+ public Thread2(Shared mem) {
+ super();
+ this.mem = mem;
+ }
+
+ public void run() {
+ int xrand;
+ int m;
+ while (true) {
+ synchronized(mem) {
+ m = mem.get();
+ if (m >= 6 && m <= 10) {
+ xrand = (int) (Math.random() * (10-1+1)+1);
+ System.out.printf("\t\t\tThread 2: (%d, %d)\n", m, xrand);
+ mem.set(xrand);
+ mem.notifyAll();
+ } else {
+ try {
+ mem.wait();
+ } catch(InterruptedException e) {
+ e.printStackTrace();
+ break;
+ }
+ }
+ }
+ }
+ }
+}