Win a copy of Rust Web Development this week in the Other Languages 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

thread

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<pre>
public class Whiffler extends Object implements Runnable
{
Thread myT;
public void start()
{
myT = new Thread(this);
}
public void run()
{
while(true()
(
doStuff();
}
System.out.println(Exiting Run");
}
public void doStuff()
{
// some code
}
// some more code...
</pre>
Assume that above code compile free and also assume that a Java application creates a Whiffler object and calls start[/] method, that no other calls to Whiffler methods are made and that the [b]Thread in this object is the only one the application creates.
Which of the following are true ?
1. The doStuff method will be called repeatedly.
2. The doStuff method will never be executed.
3. The doStuff method will execute at least one time.
4. The statement in line 10 will never be reached.
Answer is 2&4.
Since the statement says that Whiffler oject calls start method, why not answer 1&3.
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<pre>
public void start(){
myT = new Thread(this);
}
</pre>
The start() method of the Whiffler class just creates a new thread. It is not started yet.In order to start the thread you have call the start() method of the thread ref myT such as myT.start() . Also note that, the public void start() method defined in this Whiffler class is not inherited from Thread class. It's not at all related to starting the thread. It is completely diff. method which happened to have the same name as start() method in Thread class. The Whiffler class is not a thread by itself. It just implements the Runnable interface.
regds
maha anna
 
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
Thanx....Anna!!!(I came to know u r a she!!....then how come ur name in He.....thinking that it is a Indian name)
 
Slideshow boring ... losing consciousness ... just gonna take a quick nap on this tiny ad ...
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic