• Post Reply Bookmark Topic Watch Topic
  • New Topic

How wait() & notify() works in Multhithread environment?  RSS feed

 
aastha malik
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kindly explain the flow of this program?

public class ThreadA {
public static void main(String [] args) {
ThreadB b = new ThreadB();
b.start();

synchronized(b) {
try {
System.out.println("Waiting for b to complete...");
b.wait();
} catch (InterruptedException e) {}
System.out.println("Total is: " + b.total);
}
}
}

class ThreadB extends Thread {
int total;
public void run() {
synchronized(this) {
for(int i=0;i<5;i++) {
total += i;
}
notify();
}
}
}

Thanks
Aastha
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Here are a couple of suggestions to get better responses on this forum.

  • Use code tags. Click Here
  • Format your code (indent syntactically)
  • Ask a specific question. "Explain the flow of this program" - not so good. "When does the thread stop processing and return to main?" -- better


  • So, what do you think the flow is, or what specifically are you confused about?

    (Here is the formatted code. It's much easier to read.)

     
    Henry Wong
    author
    Sheriff
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Knute Snortum wrote:
  • Ask a specific question. "Explain the flow of this program" - not so good. "When does the thread stop processing and return to main?" -- better


  • So, what do you think the flow is, or what specifically are you confused about?


    Thanks Knute. Have a cow.


    Agreed, we have no idea of what the confusion is. As the OP probably already know, threading is a big topic, so, to explain stuff without any context of what is the confusion ... well... the OP is probably better off with starting off with a tutorial on threads.

    Henry
     
    aastha malik
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for the suggestion Knute.

    Actually i am confused with the flow of wait and notify? Like first notify is called() or wait() ? could you explain with the context of the program i mentioned.

    Thanks in advance..
     
    Henry Wong
    author
    Sheriff
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    aastha malik wrote:
    Actually i am confused with the flow of wait and notify? Like first notify is called() or wait() ? could you explain with the context of the program i mentioned.


    The Java Language Specification doesn't have requirements of how two threads are scheduled. In this example, it is actually possible for the notify() methoid to be reached first -- it is dependent on the underlying threading environment. Having said that, it does take time to start a thread, so the wait() method should be reached first in most cases.

    Regardless, it may be possible for notify() to be reached first, and hence, this is arguably a bad example.

    Henry
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!