• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread with infinite loop

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers,
Here below is a question from Mock exam


1.The code does not compile - "nt2.run() is never reached"
2 .The code compiles and runs 3 non ending non demon threads.
3.The code compiles but runs only 1 non ending, non demon thread.


Given answer is 3 but i was thinking it should be 2

As I understand, main() will continue with its next statements after starting the thread.so other 2 thread should run after the first one.

Please anybody can explain this??
Thanks in advance
--------
Shakunthala
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are not even invoking a Thread..

calling run of the Runnable Target will not create a new thread of execution..

you do this...
Thread t=new Thread(nt1)
t.start()

and you should call the run direct if you wanna create a new thread...

hope help
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shakuntala,

Main thread is a non demon thread which will start when you execute your program. You can observe that their is no thread is started.

I.e Thread t=new Thread();
t.start();

And hence run method is called.

when object reference nt1 executes the run method, method runs into a infity loop, and control is never returned to main method and hence main method which is a non demon thread runs for ever.

Hope this helps.
 
shakunthala Divakar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it.Thank you vey much
reply
    Bookmark Topic Watch Topic
  • New Topic