Hi everyone,
Consider below code,
package thread;
public class Noti5 extends Thread{
Calc c;
Noti5(Calc calc){
c = calc;
}
public void run(){
synchronized(c){
try{
System.out.println("waiting for calculation..");
c.wait();
}catch(Exception e){}
System.out.println("Total is.."+c.total);
}
}
public static void main(String[] args) throws InterruptedException {
Calc calculator = new Calc();
new Noti5(calculator).start();new Noti5(calculator).join();
new Noti5(calculator).start();
new Noti5(calculator).start();
calculator.start();
}
}
class Calc extends Thread{
int total;
public void run(){
synchronized(this){
for(int i=0;i<10;i++){
total += i;
}
notify();
}
}
}
1.if we didnt use "c.wait" & "notify" in above code then it produces different results.
what is the use of this both?
i have basic understanding of both,but couldnt find out satisfactory answer.
2.can we use notify() without wait() or wait() without notify()?
3.what happens if we call notify() first and then code reads wait()?
Kindly correct me if im wrong.
Thanks in advance.
surya55 0 Newbie Poster
stultuske 1,116 Posting Maven Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.