File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can notify() be called on a thread that has not started yet?

 
Ajay Xavier
Ranch Hand
Posts: 109
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

can notify() be called on a thread that has not started but initialized?

for eg.

Thread t = new Thread(MyRunnable);
sychronized(t)
{
t.notify();
}

.
.
.
.
t.start();


will this work?
 
Edward Harned
Ranch Hand
Posts: 291
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you do that, NO.

Will it work, NO.

start() does more than execute the run() method.
 
Ajay Xavier
Ranch Hand
Posts: 109
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what will happen if notify() is called before starting the thread?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Edward]: Can you do that, NO.

I believe this should be, YES.

[Ajay]: what will happen if notify() is called before starting the thread?

Nothing. That is, you can call notify(), and the method will execute almost immediately, with no effect. Any time you notify() something that's not in a wait(), the notify() is simply ignored. You can observe this yourself by writing a simple test.
[ May 31, 2006: Message edited by: Jim Yingst ]
 
Ken Blair
Ranch Hand
Posts: 1078
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Edward Harned:
Can you do that, NO.

Will it work, NO.

start() does more than execute the run() method.


Wrong. It will work perfectly fine, it just won't have any useful effect because in that code at no point is any thread in the wait set of t. You seem to be making the same error the OP is, that is in thinking that wait() and notify() have anything to do with Thread or anything to do with start().

wait() and notify() are a synchronization aid that can be invoked on any Object. When wait() is invoked on an Object the current thread of execution is suspended and that thread is added to the Object's wait set. The thread will resume when it is either removed from that wait set or when it is interrupted. Invoking notify() on an Object will remove one thread from it's wait set, invoking notifyAll() will remove all threads from it's wait set.

In your code example your are invoking notify() on "t". That t is a Thread is not relevent and the state of t is also not relevent. All that happens is that you acquire the monitor for t (or rather the object pointed to by the reference variable t) and then invoke notify() on t which removes all threads from it's wait set. However, at that point it is impossible for any thread to be in t's wait set because you just created it and you have not invoked wait() on it from any thread, so it will do nothing.
 
Ajay Xavier
Ranch Hand
Posts: 109
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ken Thanks for your reply. My doubt is cleared.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic