Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a Java Thread be alive more than once?

Ok.... Let me try to explain this the best I can.... Also: this is for a mod within minecraft. Okay, so I created a thread object

public static Thread KillThread = new Thread();

Then in the constructor of my main class which is called when the game(Mine craft starts) I have

KillThread = new Thread(new KillAuraThread());

KillAuraThread is the name of the class that is the thread.. So I created a thread now. Is where it's pissing me off The thread will run for exactly 1 second, and It can not be running multiple times or it will ruin the point of the delaying and threading.

if(KillAura.enabled && !KillThread.isAlive())
    {
        System.out.println("Go AURA!");
        try
        {
            KillThread.start();
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }

That is called every tick within the game where it would send position updates and such.

Now here is where I'm having the problem. Once the thread starts it becomes "alive" and when it ends it is no longer "alive". But can threads only be started once? because after the first run it's no longer working? And ideas? Links?

like image 476
user1653457 Avatar asked Dec 16 '22 19:12

user1653457


1 Answers

Yes Threads can only be started once, you cannot reuse a Thread object.

It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution. See java.lang.Thread.start()

Regardless of this fact, do not use the Thread.State for thread lifecycle management.

like image 127
Konrad Reiche Avatar answered Jan 04 '23 10:01

Konrad Reiche