• 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

Question about thread

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
please help answering the following question

What must be true for the RunHandler class so that instances of RunHandler can be used as written
in the code below?

class Test
{
public static void main(String[] args)
{
Thread t = new Thread(new RunHandler());
t.start();
}
}

choose 2 only
(A) RunHandler must implement the java.lang.Runnable interface
(B) RunHandler must extend the thread class
(C) RunHandlermust provide a run method declared as public and returning
(D) RunHandler must provide an init() method

i say a and b correct
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, you are right.

regards,
vijay.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. RunHandler must be a Runnable object. It does not have to extend from the Thread class.

Henry
 
mohamed ewis
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi henry

if i create class that extend thread i can throw it as thread parameter
as you give him a runnable object is not true ?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by mohamed ewis:
hi henry

if i create class that extend thread i can throw it as thread parameter
as you give him a runnable object is not true ?



Yes. The Thread class implements the Runnable interface, so if you extend from the Thread class, your new class can be used as the Runnable parameter in the Thread class constructor.

But you don't have to do that... You can just create a class that extends the Object class, and implement the Runnable interface.

Choice B states that you *must* extend the thread class, and that is not true.

Henry
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
However the answers are not mutually exclusive. Either a or b would be correct.

The class could implement Runnable or it could extend Thread, and, finally, though completely illogical, it is possible to extend Thread and implement Runnable at the same time.

So, as far as I can see, the answer is correct.
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Isn't RunHandlermust provide a run method declared as public and returning this option right.

Plz explain
Asha
reply
    Bookmark Topic Watch Topic
  • New Topic