views:

74

answers:

4

I was trying a example of isAlive() method of java threading. But i found that isAlive() method is returning false even if thread has been already started. Can someone please tell me what am i doing wrong? Here is the code snippet.

package app;

public class ThreadAliveDemo {

    public static void main(String[] args) {

        Thread myThread;

        myThread = new Thread()
        {
            public void run()
            {
                            Thread.sleep(3000);
                System.out.println("My Thread.");
            }
        };

        myThread.setName("My Thread");
        myThread.start();

        if(!myThread.isAlive())
        {
            myThread.setName("My Thread");
            myThread.start();
        }

    }

}
+5  A: 

There's a good chance the thread will have started, executed, and finished, between your call to start() and your call to isAlive().

Java offers no guarantees on the sequence in which these things happen. It could execute the spawned thread immediately, or it may choose to defer it until a bit later.

Incidentally, your code is trying to re-start the thread after it has died. This is not permitted:

It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.

So calling start() after checking isAlive() is never going to work.

skaffman
Aye... I need to wrap a `synchronized` block around my brain and my fingers
skaffman
No, i tried using Thread.sleep(3000); call inside the run method. Still i am getting the same issue.
Rupesh Chavan
But how come? i have made my thread sleep for 3 secs. and then checked the isAlive() call. And i think in this case isAlive() call must return true but it is still returning true result.
Rupesh Chavan
@Rupesh: How do you know what value it's returning? Your code inside the `if` block doesn't do anything.
skaffman
I have placed breakpoint at if block and then checked what value does myThread.isAlive() returning.
Rupesh Chavan
@Rupesh: And you can do all of this in 3 seconds? Using a manual debugger is only going to make your timing problems worse. Your code, as written, is already broken (as pointed out), and you you *really* need to avoid using coincidence to make sure your thread timings work as expected.
skaffman
Sir consider the 3 seconds sleep time as the time required to complete the execution of child thread. My concern is about why am i getting improper results after isAlive() call.
Rupesh Chavan
@Rupesh: I understand that. What I'm telling you is that program is *fundamentally flawed*. You can't do that sort of loose coding with threads and expect to get useful results.
skaffman
I agree. Thanks for the useful comment.
Rupesh Chavan
+1  A: 

I haven't done any multithreading in java yet, but it looks to me like your thread probably will have run and exited before the isAlive() check. After all, looks like your thread just prints something out and then dies.

BobTurbo
+2  A: 

If my memory serves me well java has quite long periods between thread switching so it is possible that the isAlive fails because the thread is not yet alive. Try to add some waiting time between thread.start() and thread.isAlive()

dbemerlin
I tried making thread sleep for 3 seconds but getting the same problem.
Rupesh Chavan
try to make the calling thread sleep for a second (_and_ let the child thread sleep 3 seconds)
dbemerlin
it has worked perfectly.. Thanks a lot.
Rupesh Chavan
+1  A: 

I don't see the point of the code you have posted. Thread.start() starts the thread: you don't need to start it twice. I don't see how your code can realistically into a situation where it has a Thread and doesn't know whether it has been started or not; anyway there are plenty of ways to code around that so it can't happen.

EJP
It's not about using the code as is in a real life situation but it is about making the concepts clear.
Rupesh Chavan
But all it means is that the thread hasn't actually started when start() returns. It's asynchronous you know. I don't see what concept is being illustrated.
EJP