• 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Thread

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what must be true for the RunHandler class in this code???
class test{
public static void main(String[] arg){
Thread t=new Thread(new RunHandler());
t.start();
}
choices:
a.RunHandler must implement java.lang.Runnable interface.
b.RunHandler must extend the Thread class
c. RunHandler must provide a run method declared as public and returning void.
answer is a and c
my view points:
code will work if RunHandler implement Runnable interface or extend Thread class.so a and b are not correct.if the RunHandler extends Thread it implements Runnable interface so it is not a must that RunHandler should have a run method.
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, as you you're self just wrote: "if it extends Thread, it implements Runnable", so in that case a) is still correct.
It needs a run-method for start() to work. Thread includes one by default, so by extending Thread, you will have a class that includes a run-method as described.
I think a & c IS coorect.
/Mike
 
jeena jose
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Mikael.
sometimes we miss points that we know.
so i repeat once again:
when we extends Thread class we are implementing Runnable interface also and RunHandler is getting a run method by inheritance.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi jeena,

when a thread start executing it executes the run() method.
There are two different ways to execute ( ie thru which object).
1. The thread can execute its own run method
In this type you need to sub class the thread class and add your run() method in the sub class.
for eg
public class runhandler extends thread{
public void run(){
system.out.println("This is type 1 ");
}
}

To execute the above run method you need to instantiate the
run handler cass and invoke its start() method
for eg
runhandler rh = new runhandler();
rh.start();

2. The thread can execute the run() method of some other object
The only diff between 1 & 2 is to specify which object owns the run method.
For eg

Public class runhandler implements runnable{
public void run(){
System.out.println(" This is type 2);
}
}

In order to execute the run() method of type 2 , you need
to instantiate the class runhandler and pass it to the constructor of thread

for eg
Runhandler rh = new run handler();
Thread t = new thread(rh);
t.start();

So as per the above procedure the answer for your question is Both a and c .Hope this is right.If this is wrong, Please correct me.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this question is all down to the wording used.
To be pedantic.. I would just say (a) is the correct answer as a method comforming to (c) could still be invalid. i.e
public void run(boolean b)
 
You don't like waffles? Well, do you like this tiny ad?
ScroogeXHTML - a fast and small RTF to HTML5 and XHTML converter library
https://coderanch.com/t/742149/ScroogeXHTML-RTF-HTML-XHTML-converter
reply
    Bookmark Topic Watch Topic
  • New Topic