Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread program not giving result

 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
  •  
    Shiva Mohan
    Ranch Hand
    Posts: 486
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 486
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    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
    Posts: 486
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Please anyone answer me.i am waiting for the reply since morning.
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 7729
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    "i am waiting for the reply since morning."


    Ease up How much are you paying for this consultancy service?
     
    Henry Wong
    author
    Marshal
    Pie
    Posts: 20892
    75
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    (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 ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic