swag antiswag swag antiswag - 5 months ago 11
Java Question

Why is my thread dead when I run it?

I am trying to do something with my thread when it is dead, but it seems that right when I run it it is already dead...why?

new Thread(){
public void run(){
super.run();

System.out.println(isAlive());//prints out false
}
}.run();

Answer

Solution:

.run() //is not a valid way to start a Thread.

change to

.start();//Valid way to start a thread

Heres the Thread API link:

https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html

I think you got confused between the Runnable run() and the Thread start().

Heres the Runnable Interface API:

https://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.html