• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Threads

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
This may b simple but I have couple of questions.....
It prints Starting main,Main returns and the method never exits.So my question is how the flow works and how many threads are created here?
When printed activecount it prints 4.
So somebody plz explain this..
public class Q extends Thread
{
public void run()
{
for( ; ; );
}
public static void main(String args[])
{
System.out.println("Starting main");
new Q().start();
System.out.println("Main returns");
}
}
Thanx
Rajani

[This message has been edited by Rajani Katta (edited August 14, 2001).]
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Hmm, strange, Main returns shouldnt be printed since the method never finishes. As far as I know, there should be only one thread.
Try this:

this code shows you that you have only made one thread and it shows that the for loop will never end. The message Main returns should also not get printed. (Check this code to see if it compiles i havent )
Someone please correct me if im wrong, and also please answer my question above (in the code) about sleep and wait
Thank You,
Kamil.
[This message has been edited by Kamil Dada (edited August 14, 2001).]
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wonderful Question.
Here is my thought..
Generally runtime environment has both user-threads and daemon-threads. As long as a user-thread is alive the execution does not
terminate. Daemon-threads are like server, their sole purpose is to serve user-threads
Now in our case, the user-thread is automatically created to execute the main() method (aka main thread).. Any other threads
that are created from the main() are the child threads and they
inherit the main() status also.
Parent main thread executes and finishes early.. but the child thread is still active even though the main is done... this will make the daemon to be up and running, resulting in hanging situation

Hope this helps
-Ragu
 
Mini Pilla
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raghu,
Thanx.That was wonderful answer too.
But can u tell me exactly how many Threads are present in the above program...
Thanx'
Rajani
[This message has been edited by Rajani Katta (edited August 14, 2001).]
 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Altogether 2 threads. The main thread and the Q thread spawned in the main method of the main thread.
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,
Here in the run method,the for loop will run forever as the second argument is by default true, so it is a infinite loop and equivalent to:
for( ; true ; );
Try changing it to:
for( ; false ; );
and see it will just print the two statements and loop never runs.
HTH
--Farooq
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajani,
Acutally there are three!! threads

Daemon thread (1)
User-threads (2) (ie main thread and our Q.start())
Hope that helps you
Ragu
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!