This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Can notify() be called on a thread that has not started yet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Can notify() be called on a thread that has not started yet?" Watch "Can notify() be called on a thread that has not started yet?" New topic
Author

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

Ajay Xavier
Ranch Hand

Joined: Jan 03, 2005
Posts: 109
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

Joined: Sep 19, 2005
Posts: 291

Can you do that, NO.

Will it work, NO.

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


Ed's latest article: A Java Parallel Calamity http://coopsoft.com/ar/Calamity2Article.html
Ajay Xavier
Ranch Hand

Joined: Jan 03, 2005
Posts: 109
what will happen if notify() is called before starting the thread?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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 ]

"I'm not back." - Bill Harding, Twister
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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

Joined: Jan 03, 2005
Posts: 109
Ken Thanks for your reply. My doubt is cleared.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can notify() be called on a thread that has not started yet?
 
Similar Threads
Difference between Notify and NotifyAll
wait( ) , notify( ) , notifyAll( ) methods ...
thread
Q on Kathy's Mock Test (Tpoic:Threads)
thread