diff options
Diffstat (limited to 'Year_3/TSDWL/ex_20131219/Thread1.java')
-rw-r--r-- | Year_3/TSDWL/ex_20131219/Thread1.java | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/Year_3/TSDWL/ex_20131219/Thread1.java b/Year_3/TSDWL/ex_20131219/Thread1.java new file mode 100644 index 0000000..b9f8d84 --- /dev/null +++ b/Year_3/TSDWL/ex_20131219/Thread1.java @@ -0,0 +1,31 @@ +public class Thread1 extends Thread { + private Shared mem; + + public Thread1(Shared mem) { + super(); + this.mem = mem; + } + + public void run() { + int xrand; + int m; + while (true) { + synchronized(mem) { + m = mem.get(); + if (m >= 1 && m <= 5) { + xrand = (int) (Math.random() * (10-1+1)+1); + System.out.printf("Thread 1: (%d, %d)\n", m, xrand); + mem.set(xrand); + mem.notifyAll(); + } else { + try { + mem.wait(); + } catch(InterruptedException e) { + e.printStackTrace(); + break; + } + } + } + } + } +} |