• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

waiting thread is invoked without calling notify()

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everybody,

Please check the below code

class MyThread
{
public static void main(String[] args)
{
ThreadB t2=new ThreadB();
t2.start();
synchronized(t2)
{
try
{
t2.wait();
System.out.println("After waiting complete "+ t2.getPriority());
}
catch(InterruptedException e)
{
System.out.println("Exception thrown");
}
}
}
}
class ThreadB extends Thread
{
int total;
public void run()
{
System.out.println("Inside run");
}
}

and it's output is

Inside run
After waiting complete 5

I'm not able to understand one thing that I'm not calling notify() or notifyall() anywhere in this code. But still I'm getting the output "After waiting complete 5".
Kindly explain this.

Regards
Manju
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manju,

We have a simple rule here. Please Quote Your Sources.
 
Manju Rao
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Kenneth for correcting me.

I was trying to understand the flow of program sent by Aparna Misri in one of todays post at Javaranch. She had picked it from K&B book Chapter threads. I then made many changes in the program and not able to understand this thing that without calling notify how a waiting thread started.

Regards
Manju
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manju this is an exceptional case which works just like join method. If you call wait on a thread object which is still in runnable state, then when that thread completes execution, then the wait call will be notified automatically.

Eg



This happens because of the internal implementation of run method in Thread class and as far as I remember Henry said that it will not be a part of the exam as this behavior is not guaranteed...
 
Manju Rao
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit for clearing this.

I just want to know one more thing that what "when that thread completes execution" means. Is this mean that when code inside run method completed?

Regards
Manju
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manju Rao:
I just want to know one more thing that what "when that thread completes execution" means. Is this mean that when code inside run method completed



Yes a thread completes when it's run method completes execution. In case of main thread, it completes execution when main thread completes execution...
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Manju,
Please go through this discussion first, will give some ideas.

Two thumb rules here Manju.

1. What happens when a thread does not call notify()?
You call it Spurious wakeup. That's a thread can wakeup itself in such a condition. Thats's why you got this output. Again, this behavior is not guranteed.So, dont rely on it.

2. What happens when a thread calls notify() and notifyAll(), but there are no threads to be notified?
You loose nothing; the notify() simply returns without doing anything great.

Hope, it helps.
 
Manju Rao
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Manni and Ankit for the reply.

Regards
Manju
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic