Home Answers Viewqa Java-Beginners (help me) use wait() and notify() method in Thread.

 
 


siti umairah
(help me) use wait() and notify() method in Thread.
1 Answer(s)      a year and a month 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

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 Pages:

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.