• 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 Again

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

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.




how come the answer is 3! i thought thread scheduler would give the three threads their turn. is this because they are the objects of same class? i just don't get it....someone pls explain

( tags)
[ June 08, 2005: Message edited by: Barry Gaunt ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is here:

Because nt1.start() was never called, just run() - the thread scheduler is never involved. The run() method is called as part of the original, main thread. And it enters an infinite loop, preventing any other subsequent method calls from executing. If you replace "nt1.run()" with "nt1.start()" wyou will see very different results.
 
Sajid Moinuddin
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry..my bad...silly question...i somehow misinterpreted run with start....God...what was i thinking!!
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sajid,
Actually what happen with your code, it initiate the first thread that runs endlessly. If you try your code to run with a loop of 10 then it will work perfectly. Since first thread run indefinitely, second and third thread will never get a chance to take the cpu. Although what you are saying would be correct if your OS implements timesharing algorigthm, which give CPU time in a round robin fasion to all the threads. But as with multithreading we can't rely on OS algo. The correct answer is option 3.
If you still have doubts or if you feel I am wrong, please do correct me.
Regards,
Atul
 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because its the main thread(which gets started when main method execution
starts) that calls the run method and not a brand new thread instance.
Calling run method inside main method with a instance of the class that implements Runnable without an instance of Thread is just like calling any other normal method. Hence control transfers from main method to run method. And since there is a while loop whose condition is true inside the run method
that particular thread will execute for ever.
 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is ok that we are no creating the thread , but why the compiler is not giving error of unreachable code , as the second run method is not going to called ever.
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
The problem is here:

Because nt1.start() was never called, just run() - the thread scheduler is never involved. The run() method is called as part of the original, main thread. And it enters an infinite loop, preventing any other subsequent method calls from executing. If you replace "nt1.run()" with "nt1.start()" wyou will see very different results.



i think in this ...we are not creating any new thread at all ...except main which exists...

in oreder to create thread we must create thread like

Thread a =new Thread(nt1);

is i m rt...i think there is no other way for creating thread...
how

nt1.start() will run ???
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by amit taneja:

i think in this ...we are not creating any new thread at all ...except main which exists...

in oreder to create thread we must create thread like

Thread a =new Thread(nt1);

is i m rt...i think there is no other way for creating thread...
how

nt1.start() will run ???



that is the second issue. Since we are not creating any object of the Thread class, we are not going to get any new threads either.
But given the code and the problem, the call to run() will be like a normal method call and go in an endless loop right there.

As to why it would compile, the call is a syntactically correct method call. The complier is not going to actually go through the run() method to find out that there is an endless loop and hence throw an Unreachable Statement Exception.
 
Look ma! I'm selling my stuff!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic