• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

where is the notify?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Guys,

I have a question:
In the below code, we provide a simple function. When the user inputs "q" then exit from the application, otherwise wait for the user's input. My question is I find the wait, but where the application notify the wait? thanks in advance.




 
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

My question is I find the wait, but where the application notify the wait?



There is a wait() method call in your application, but what does it do? Specifically, what is the criteria that causes the application to call the wait() method?

Henry
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<won't interrupt Henry...>
[ November 04, 2008: Message edited by: Steve Luke ]
 
donghua shi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Henry,
thanks for you reply.

Acctually this code implements this function:
1. we can imput anything whatever you want
2. if we input "q" then quit the application

So it seems that when we input a character, the "wait" code is notified. My doubt is:
we don't do any notify operation explicitly, how the "wait" code is notified?
 
Henry Wong
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


So it seems that when we input a character, the "wait" code is notified. My doubt is:
we don't do any notify operation explicitly, how the "wait" code is notified?



Let's forget about notify() for a minute. Let's take a look at the wait() method call... Take a look at the code again...



When is the wait() method being called? Answer: when the string "line" is equal to null. So... When is "line" ever equal to null?

Henry
 
donghua shi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Henry,

so the "wait" statement doesn't take effet at all, is it right? In the code sippet, it just loop indefinitely. If no input, it just blocks in the "readline()" statement.

I think i am misguided by the "wait()" statement.

thanks.

Stone
 
Henry Wong
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup. The wait() method is not called.

Henry
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All, I have a doubt related to this wait() and notify(). Please look at the following code:

1. class ThreadA {
2. public static void main(String [] args) {
3. ThreadB b = new ThreadB();
4. b.start();
5.
6. synchronized(b) {
7. try {
8. System.out.println("Waiting for b to complete...");
9. b.wait();
10. System.out.println("After waiting");
11. } catch (InterruptedException e) {}
12. System.out.println("Total is: " + b.total);
13. }
14. }
15. }
16.
17. class ThreadB extends Thread {
18. int total;
19.
20. public void run() {
21. synchronized(this) {
22. for(int i=0;i<100;i++) {
23. total += i;
24. }
25. notify();
26. }
27. }
28. }

In line number no 25, the notify() method is included. If I remove this notify(), will the thread that invokes the wait() method will wait for ever??.

I tried removing the notify() method. I get the same results irrespective of the presence of notify() method.

Output(Irrespective of whether I include notify() or not:

Wating for b to complete
0
1
3
6
10
15
21
28
36
45
After waiting

Please explain whether the notify() method is mandatory if we call the wait() method or once the called thread is completed, will the waiting method resume by itself..

Thanks
Loga
[ November 07, 2008: Message edited by: Loganathan Karunakaran ]
 
Henry Wong
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please start a new topoc for new questions. Otherwise, it can be considered as "hijacking" a topic.

Henry
 
Loganathan Karunakaran
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, sure, I will start now
 
Come have lunch with me Arthur. Adventure will follow. This tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic