summaryrefslogtreecommitdiff
path: root/Year_3/TSDWL/ex_20191218/Thread1.java
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-12-01 23:10:21 +0100
committerSanto Cariotti <santo@dcariotti.me>2021-12-01 23:10:21 +0100
commiteb4161840396b65cb8e018db042eb8ae101b2646 (patch)
tree2629f322305a639e96964d17edad39cb137a3f77 /Year_3/TSDWL/ex_20191218/Thread1.java
parent071a8ee7b742498dc4714d06eaf55f6a40aeb7fa (diff)
tsdwl: adds in java
Diffstat (limited to 'Year_3/TSDWL/ex_20191218/Thread1.java')
-rw-r--r--Year_3/TSDWL/ex_20191218/Thread1.java42
1 files changed, 42 insertions, 0 deletions
diff --git a/Year_3/TSDWL/ex_20191218/Thread1.java b/Year_3/TSDWL/ex_20191218/Thread1.java
new file mode 100644
index 0000000..55edaba
--- /dev/null
+++ b/Year_3/TSDWL/ex_20191218/Thread1.java
@@ -0,0 +1,42 @@
+public class Thread1 extends Thread {
+ Shared mem;
+ public Thread1(Shared mem) {
+ super();
+ this.mem = mem;
+ }
+
+ public void run() {
+ int m;
+
+ while (true) {
+ try {
+ Thread.sleep(100);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ break;
+ }
+ m = (int) (Math.random() * (10-0+1)+0);
+
+ synchronized(mem) {
+ System.out.printf("Thread 1, x = %d, m = %d\n", mem.get(), m);
+ if (mem.get() == -1) {
+ break;
+ } else if(mem.get() == m) {
+ System.out.println("RISPOSTA CORRETTA");
+ mem.set(-1);
+ } else if(Math.abs(m-mem.get()) > 5) {
+ try {
+ System.out.println("risposta MOLTO sbagliata");
+ System.out.println("Thread 1 going to sleep");
+ mem.wait();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ break;
+ }
+ } else {
+ System.out.println("risposta sbagliata");
+ }
+ }
+ }
+ }
+}