Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How does notify work  RSS feed

 
Shouvik Bhattacharya
Ranch Hand
Posts: 40
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Okay so here it is on line 29 I ask Main thread to wait so that MyThread can finish computing the sum in the run method and finally notify the thread waiting on MyThread that its done. I was just playing around with this code and I happen to find that even if I don't call notify on line 12 the waiting thread i.e. the Main thread resumes execution so I was wondering that is it like that it gets auto notified or something like that on account of run() getting over or is there some other meaningful explanation to the process?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know why the main thread is resuming, but I can see a reason why line 12 makes no difference. You are calling wait() on the Thread object, and notify on the Notification object. You're synchronizing on different objects.

Incidentally, if all you want to do is to wait until another thread has finished before continuing, there's a simpler way. Have a look at the join() method in the Thread class.
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shouvik Bhattacharya wrote:
Okay so here it is on line 29 I ask Main thread to wait so that MyThread can finish computing the sum in the run method and finally notify the thread waiting on MyThread that its done. I was just playing around with this code and I happen to find that even if I don't call notify on line 12 the waiting thread i.e. the Main thread resumes execution so I was wondering that is it like that it gets auto notified or something like that on account of run() getting over or is there some other meaningful explanation to the process?


The Thread instance is used internally by the Java library to wake up threads that are joining. When a thread terminates, a notifyAll() notification is sent via the thread object, so that threads that are waiting to join can continue.... basically, don't use the thread object for notifications, as the library is using that object.

Henry
 
Shouvik Bhattacharya
Ranch Hand
Posts: 40
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am finding difficulty in comprehending the fact that Matthew told regarding two different object in action....... as per my understanding goes about I feel that I am notifying on the same object on which I wanted to wait isn't it......I mean no offence to Matthew's view but I am just unable to understand I feel that as per logic and common sense the thread is supposed continue waiting unless it gets notified isn't it.......I tried google but no answer.......please help understand guys.
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shouvik Bhattacharya wrote:I am finding difficulty in comprehending the fact that Matthew told regarding two different object in action....... as per my understanding goes about I feel that I am notifying on the same object on which I wanted to wait isn't it......I mean no offence to Matthew's view but I am just unable to understand I feel that as per logic and common sense the thread is supposed continue waiting unless it gets notified isn't it.......I tried google but no answer.......please help understand guys.


Not sure how Google can help you here -- as this has nothing to do with a concept. Your code waits on one object, and notifies on another. For notifications to work, it has to be the same object -- it is bug in your application, and Google doesn't do code reviews of specific code, does it?

And as for the other response, regarding waiting on the Thread instance, you can search these forums -- as accidentally notifying on the thread instance seems to be a common mistake.

Henry
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

By the way, with two-line changes, at lines 8 and 10...



You can solve both problems. With the changes, the threads now synchronize on the same object, and doesn't use the thread object (and hence, conflict with the core library).

Henry
 
Shouvik Bhattacharya
Ranch Hand
Posts: 40
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!