aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes special 'rule' ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "special Watch "special New topic
Author

special 'rule' ?

Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
All
here's a snippet:-


In my knowledge ( )
There are only 2 ways to define code to run in a separate thread :-
■ Extend the Thread class.
■ Override the run() method.

The 1st may look like:-

& the 2nd may look like:

BUT..in the code snippet attached, "public class TestThreads " doesn't do either of the two & happily creates a thread 't' which then goes about it's business.
My question:- how can class "public class TestThreads" go scot free, without following the 2 rules ?
TIA
[ October 15, 2004: Message edited by: Barry Gaunt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Netty, please use [C0DE][/C0DE] tags to surround formatted code. It helps people to understand your question more easily.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I think you mean that the two ways are:
  • Extend Thread and override its run method.
  • Implement Runnable by providing it with a run method, and passing the resulting Runnable object to a Thread to run.


  • The code:


    is creating an object of a class which extends class MyThread (which is itself a Thread). This "anonymous" local class has been provided with a run method which overrides that provided by class MyThread.
    [ October 15, 2004: Message edited by: Barry Gaunt ]
    Netty poestel
    Ranch Hand

    Joined: Sep 20, 2004
    Posts: 131
    Thanks Barry

    getting nearer to it but still not completely. The code example is fine with the explanation
    "...is creating an object of a class which extends class MyThread (which is itself a Thread). "

    so we have this now :-

    [C0DE]
    public class SyncTest {
    public static void main (String [] args) {
    Thread t = new Thread() {
    Foo f = new Foo();
    public void run() {

    }
    };
    t.start();
    }
    }
    [/C0DE]

    this compiles fine. but as I see it, it's not creating any object of any class which is extending a class which is itself a Thread.[ huh ?]
    what I mean is, in 1st mail thread "Thread t = new MyThread() " was fine because MyThread was extending Thread.

    but in the 2nd e.g class SyncTest is not doing so. so [ and I'm guessing here] does "Thread t = new Thread()" get it's thready genes from the main method ?

    Thx. for any help.
    Barry Gaunt
    Ranch Hand

    Joined: Aug 03, 2002
    Posts: 7729
    We're nearly there with the code tags. Replace the zero characters in C0DE with "oh" (0->O) and it will work

    Where did classes Foo and SyncTest come from? Let's stay with the original code.

    This part of the code:


    is equivalent to this code:


    Is that clearer?
    Barry Gaunt
    Ranch Hand

    Joined: Aug 03, 2002
    Posts: 7729
    Let's have a look at this code now:


    I've deleted the Foo bit because it's not relevent to this at the moment.

    Here we are creating an object from a new (unnamed) class extending Thread. This unnamed class is a Thread (because it extends Thread). The run method (which does not do much) is in this new unnamed class and it overrides the one in the Thread class.

    The equivalent code is:
    Netty poestel
    Ranch Hand

    Joined: Sep 20, 2004
    Posts: 131
    Very clear now Barry...

    wonder why the books eat up these 'secret agent' parts when parting the knowledge.Hmm....so that was that.

    and Thx. for pointing out that 'C0de' part..[if only there was a blush icon available ]

    Thx!
    Barry Gaunt
    Ranch Hand

    Joined: Aug 03, 2002
    Posts: 7729
    I write C0DE instead of CODE so you see the tags as [C0DE][/C0DE] and not replaced as

    [ October 16, 2004: Message edited by: Barry Gaunt ]
    Netty poestel
    Ranch Hand

    Joined: Sep 20, 2004
    Posts: 131
    Ok Thx.

    and lest I forget ...A Great weekend to you Barry and All on this Ranch !


     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: special 'rule' ?