File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread program not giving result Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread program not giving result" Watch "Thread program not giving result" New topic
Author

Thread program not giving result

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 479



I was expecting the answer:false running.But when i execute the above program,it displayed nothing.why?

Another doubt:the TSamp class extends Thread.so,Is the TSamp need to override run() method in it?Because in the API,it mentoined that
the subclasses of Thread should override run() method.Please help.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Here are some things to consider in finding your answers...
  • Which of Thread's constructors is called when TSamp is instantiated? (What happens to the Runnable reference?)
  • If you don't override run() when extending Thread, what will run() do when it's called?


  • "We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
    sscce.org
    Shiva Mohan
    Ranch Hand

    Joined: Jan 05, 2006
    Posts: 479
    Thank you very much marc.

    Which of Thread's constructors is called when TSamp is instantiated?
    TSamp run()m method is called.

    What happens to the Runnable reference?
    Objective2425 m = new Objective2425();
    Thread e=new Thread(m);
    when i called e.run(),Objective2425 run() is executed.

    Please help me with the last question.
    if you don't overide run() when implementing Runnable,that will give compile error.
    If you don't override run() when extending Thread, what will run() do when it's called?
    [ January 05, 2007: Message edited by: Shiva Mohan ]
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    Remember: In a constructor, if the first line is not an explicit call to this (an overridden version of the constructor) or super (with the desired arguments), then there is an implicit call to super with no arguments.

    The constructor for TSamp takes a reference to a Runnable, but it doesn't do anything with it. The constructor's body is empty, and so there is only an implicit call to super with no arguments. Therefore, the Thread instance is not created with a Runnable. If you check the API for Thread to see what run() does in this situation, you will see why this program does not display anything.

    If you add super(m); in TSamp's constructor, then you will get different results.
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    Originally posted by Shiva Mohan:
    ... Please help me with the last question.
    if you don't overide run() when implementing Runnable,that will give compile error.
    If you don't override run() when extending Thread, what will run() do when it's called?

    Runnable is an interface with only one method: run. So any (non-abstract) class implementing Runnable must implement the run method or there will be a compilation error.

    On the other hand, Thread is a class that already implements Runnable. The run method in Thread behaves differently depending on whether the Thread was created with another Runnable object. (See the API.) This explains why there is no output from the above code.

    When extending Thread, you are not required to override the run method because it's not abstract. But if you don't override run, and the Thread is not created with another Runnable object, then very little will happen when run is called (as the code above demonstrates).
    Shiva Mohan
    Ranch Hand

    Joined: Jan 05, 2006
    Posts: 479


    i got couple of questions again on this.

    (1)output of the above program is always comes like
    taxman
    Thread-0
    geography
    Thread-1

    since this.start() is in go() method.or the order is undetermined?

    (2)how to change the Thread-0 and Thread-1 as taxman thread and geography thread since Objective71(String name) consturctor class super() implicitly.then that call Thread()---This constuctor is same as Thread(null,null,gname).How could we get taxman as a new thread.Please explain.I thought ,setting the constructor Objective71("taxman") would set the thread as taxman thread at first.

    [ January 23, 2007: Message edited by: Shiva Mohan ]
    [ January 23, 2007: Message edited by: Shiva Mohan ]
    Shiva Mohan
    Ranch Hand

    Joined: Jan 05, 2006
    Posts: 479
    Please anyone answer me.i am waiting for the reply since morning.
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    In the main thread, swb1.go() is called. Within go, swb1's start is called. At that point, swb1 is eligible to run as a separate thread, but exactly when this happens depends on platform-dependent thread scheduling. Back in the main thread, swb2 is created, then swb2.go() is called, and swb2's start is called. By this time, it's quite likely -- although not guaranteed -- that swb1's run method has been given a chance to execute, and its output is already on your screen.

    As you noted, a no-args super() is implicitly called unless super is explicitly called with different arguments. So to invoke the overloaded superclass constructor, Thread(String name), all you need to do is call super(sName) as the first line of the subclass constructor.
    Barry Gaunt
    Ranch Hand

    Joined: Aug 03, 2002
    Posts: 7729
    "i am waiting for the reply since morning."


    Ease up How much are you paying for this consultancy service?


    Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
    Getting someone to think and try something out is much more useful than just telling them the answer.
    Henry Wong
    author
    Sheriff

    Joined: Sep 28, 2004
    Posts: 18962
        
      40

    (2)how to change the Thread-0 and Thread-1 as taxman thread and geography thread since Objective71(String name) consturctor class super() implicitly.then that call Thread()---This constuctor is same as Thread(null,null,gname).How could we get taxman as a new thread.Please explain.I thought ,setting the constructor Objective71("taxman") would set the thread as taxman thread at first.


    So you know that there is another constructor of the Thread class that does *exactly* what you want. But instead of calling it from your constructor, you just assume that the threads no-arg constructor is the same, and change something unrelated in your constructor...

    And somehow the Java compiler will know to call that instead? Why don't you help the Java compiler, by telling it which constructor you want to call instead?



    Henry
    [ January 24, 2007: Message edited by: Henry Wong ]

    Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Thread program not giving result