Results 1 to 17 of 17

Thread: Current thread ! current thread!

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Resolved Current thread ! current thread!

    Code comes up with main as the thread name. Even though the main thread is still running dosen't Thread.currentThread(); see the context in which it is being called?
    Code:
    class T extends Thread{
     public T(String tname){
      super(tname); 
     }
     public void run(){
      Thread t = Thread.currentThread();
      String tname = t.getName();  
      boolean isalive = t.isAlive(); 
      for(int i = 1; i <= 10; ++i){
       System.out.println(tname + " is alive ?" + " : " + isalive); 
      }
     }
    }

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Current thread ! current thread!

    How are you calling the thread? Calling isAlive on the current thread is quite futile, you know...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Current thread ! current thread!

    The thread is being started from the main method. I don't know why calling isAlive() would be futile.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Current thread ! current thread!

    The action of querying the current thread's alive status requires the thread to be alive. Which implies that isAlive will invariably return true.

    I'd still like to see the creation code.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Current thread ! current thread!

    Posted by CornedBee

    The action of querying the current thread's alive status requires the thread to be alive.
    I don't quite follow. Why would the thread being alive be a requirment to call isAlive()? If the run() method is finished executing then isAlive() simply returns false.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Current thread ! current thread!

    As long as a thread is executing, it's alive. And if it's not executing, it cannot call isAlive() on any thread object, most definitely not the one returned by currentThread(), which represents itself. It is therefore a requirement that a thread is executing if it is to call isAlive() on the value returned by currentThread(), thus implying that isAlive() will return true.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Current thread ! current thread!

    Posted by CornedBee

    As long as a thread is executing, it's alive. And if it's not executing, it cannot call isAlive() on any thread object, most definitely not the one returned by currentThread(), which represents itself. It is therefore a requirement that a thread is executing if it is to call isAlive() on the value returned by currentThread(), thus implying that isAlive() will return true.
    I don't know about that. I beg to differ. So let me ask you this. If a thread is created, set off, run() finishes executing then isAlive() is called what exception is thrown?

  8. #8
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Current thread ! current thread!

    WILL a thread references another thread have the exact same name?
    If you simply put Thread.currentThread().getName() in the system.out that will give you the correct name...but then again that doesn't makes sense if the the reference thread won't return that name either.

  9. #9

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Current thread ! current thread!

    WILL a thread references another thread have the exact same name?
    If it's an alias it should since you are simply referring to the same object.

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Current thread ! current thread!

    Quote Originally Posted by Dilenger4
    I don't know about that. I beg to differ. So let me ask you this. If a thread is created, set off, run() finishes executing then isAlive() is called what exception is thrown?
    What the hell are you talking about? Once run() finishes executing, that thread will not call isAlive() anymore!

    Another thread might then call isAlive() and it will return false. But in your snippet, you're calling isAlive() on the return value of currentThread(). This means it's the current thread, and that the current thread is alive.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Current thread ! current thread!

    Here is what you wrote. "As long as a thread is executing, it's alive. And if it's not executing, it cannot call isAlive() on any thread object"

    That sounds like you are saying just because a thread is not alive that isAlive() cannot be called on it.

  12. #12

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Current thread ! current thread!

    CB i understand that the last quote i posted refers to my code and is not a generalization because if it were you would be incorrect. Also as for my code you are pointing out the obvious and besides we are getting off base. The real question is why Thread.currentThread() refers to the main thread and not the seperate thread that was created after the main thread is spawned.

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Current thread ! current thread!

    Quote Originally Posted by Dilenger4
    Here is what you wrote. "As long as a thread is executing, it's alive. And if it's not executing, it cannot call isAlive() on any thread object"

    That sounds like you are saying just because a thread is not alive that isAlive() cannot be called on it.
    No, you're confusing caller and called in my statement. The thread which calls isAlive() must be alive. The thread on whose object isAlive() is called need not be alive.

    I'm still correct, but you're right, we're getting off-track. So I'm going to ask you for the thread creation code for the third time
    Because as it is, this code gives me the correct result:
    Code:
    class T extends Thread{
     public T(String tname){
      super(tname);
     }
     public void run(){
      Thread t = Thread.currentThread();
      String tname = t.getName();
      boolean isalive = t.isAlive();
      for(int i = 1; i <= 10; ++i){
       System.out.println(tname + " is alive ?" + " : " + isalive);
      }
     }
    
     public static void main(String[] bla) {
      new T("hello").start();
     }
    }
    The output:
    Code:
    hello is alive ? : true
    hello is alive ? : true
    hello is alive ? : true
    hello is alive ? : true
    hello is alive ? : true
    hello is alive ? : true
    hello is alive ? : true
    hello is alive ? : true
    hello is alive ? : true
    hello is alive ? : true
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  14. #14

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Current thread ! current thread!

    Posted by CornedBee

    So I'm going to ask you for the thread creation code for the third time
    You mean the second. j/k
    Posted by CornedBee

    The thread which calls isAlive() must be alive. The thread on whose object isAlive() is called need not be alive.
    Correct. We were just having trouble with semantics.

    The results that you come up with are expected. What i am concerned about is the thread name not the status. System.out.println() prints main for me and hello for you.

  15. #15

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Current thread ! current thread!

    Here is what i have.
    Code:
    public class ThreadTest{
     public static void main(String[] args){
      T t = new T("T Thread");
      threadStatus(t.isAlive(),t.getName());
      t.run();
     }
     public static void threadStatus(boolean isalive, String tname){
      String status = isalive ? "is alive" : " is not alive"; 
      System.out.println(tname +""+  status);
     }
    }
    
    class T extends Thread{
     public T(String tname){
      super(tname); 
     }
     public void run(){
      Thread t = Thread.currentThread();
      String tname = t.getName();  
      boolean isalive = t.isAlive(); 
      for(int i = 1; i <= 10; ++i){
       System.out.println(tname + " is alive ?" + " : " + isalive); 
      }
     }
    }

  16. #16

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Current thread ! current thread!

    I figured out why the thread name was main. It was becasue i was calling run() instead of calling start() on the thread.

  17. #17
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Current thread ! current thread!

    Exactly what I thought after running my test.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width