siti umairah
(help me) use wait() and notify() method in Thread.
1 Answer(s)      3 years and 2 months ago
Posted in : Java Beginners

we have to create 3 classes..1 is the main class, 1 is Thread UUM class, and another one is Thread Sintok class...we have to use wait(); and notify(); to comes out with this output:- (the output must be begin with UUM and end with SINTOK)

UUM SINTOK UUM SINTOK UUM SINTOK UUM SINTOK UUM SINTOK UUM SINTOK

Advertisement
View Answers

April 2, 2012 at 12:35 PM


class Multithreading {
int val;
boolean value = false;
synchronized int get() {
if(!value)
try {
wait();
} catch(Exception e) {
System.out.println(e);
}
System.out.println("SINTOK");
value = false;
notify();
return val;
}
synchronized void put(int val) {
if(value)
try {
wait();
} catch(Exception e) {
System.out.println(e);
}
this.val = val;
value = true;
System.out.println("UUM");
notify();
}
}

class UUM implements Runnable {
Multithreading th;
UUM(Multithreading th) {
this.th = th;
new Thread(this, "UUM").start();
}
public void run() {
int i = 0;
while(i<=5) {
th.put(i++);
}
}
}

class Sintok  implements Runnable {
Multithreading th;
Sintok (Multithreading th) {
this.th = th;
new Thread(this, "Sintok ").start();
}
public void run() {
while(true) {
th.get();
}
}
}
class WaitNotifyExample {
public static void main(String args[]) {
Multithreading th = new Multithreading();
new UUM(th);
new Sintok (th);
}
}









Related Tutorials/Questions & Answers: