• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Question on threads

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the following code,

_____________________________________________________

public class NiceThreads implements Runnable
{
public void run()
{
while(true)
{
}
}

public static void main(String args[])
{
NiceThreads nt1 = new NiceThreads();
NiceThreads nt2 = new NiceThreads();
NiceThreads nt3 = new NiceThreads();

nt1.run();
nt2.run();
nt3.run();
}
}

_______________________________________________________________
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.


Correct Answer : 3.
The code compiles but runs only 1 non ending, non demon thread.

I expected the answer to 2.

Can anyone explain me the correct answer?

Thanks in advance,
rajani.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You create a new thread by invoking start(), not run(). Invoking start() creates a new thread of execution and and invokes the run() method on that new thread. Invoking run() directly, as you've done, skips the all-important step of creating a new thread of execution.

In fact, in your example, the lines:

nt2.run();
nt3.run();

are never executed. You get caught in an infinite loop prior to that.

And, by the way, it's a "daemon" thread, not a "demon" thread.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code has two problems. The first one is the use of the run instead of the start method as pointed out by Corey. The second problem is you don't have a Thread object in your main mehtod. If you just change ntX.run() to ntX.start() you will get a compilation error.

To make the code work, you need to either:

1. Change the declaration of NiceThreads to "public class NiceThreads extends Thread" and start the threads in your main method with nt1.start(), nt2.start() ... etc.

2. Keep the NiceThread declaration as is. Change the main method code to:



Hope this helps.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic