This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of TensorFlow 2.0 in Action and have Thushan Ganegedara on-line!
See this thread for details.
Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • 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
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Question on threads

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Yeast devil! Back to the oven that baked you! And take this tiny ad too:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic