• 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

Threads

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Found this on a mock exam.....
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.
The answer given is 3 which I couldn't understand..... I thought it would be 2.....
Does a thread (which goes into an infinite loop prevent other threads from being created/run???
Any inputs are greatly appreciated.....
Thanks,
Shafeeq
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know the answer, but I'm guessing that it depends on the operating system (if it uses the time-sliced or pre-emptive model). I think a time-sliced method would interrupt the currently running thread to let other threads run.
Try adding a System.out.print() to the while loop--having each instance print a unique string--and see how it behaves.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is certainly 3 here. I should have actually looked more closely at the code first before responding!!!
Just because the class implements Runnable, no new threads are created, and even if they were, the start() method is what needs to be called!!
 
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is 3.
First you are not creating new Threads, when you implement Runnable you need to create an instance of the Thread class with the constructor with a Runnable arg. and send the class implementation of Runnable to the Thread. Then you must call the start method of that Thread class for the desire result. If you call the run method directly, you are invoking that method once, you aren�t creating a new Thread.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic