4

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?

  • http://stackoverflow.com/questions/1881714/how-to-start-stop-restart-a-thread-in-java – noahlz Sep 07 '12 at 00:54
  • (Perhaps there is a better approach here than threads?) –  Sep 07 '12 at 00:55
  • Don't use threads. Minecraft is resource intensive enough without adding tons of threads to run for one second and stop. Do it the same way everything else does, which is give it a lifetime counter, decrease it by one every tick, and when it gets to zero, kill the aura. – Wug Sep 07 '12 at 00:55
  • KillAuraThread is a Runnable interface ? – cat916 Sep 07 '12 at 00:56
  • You should just hop on irc.esper.net and visit #risucraft . It's significantly more specialized than SO. – Wug Sep 07 '12 at 00:58
  • 1
    Also, starting a thread in a constructor is common cause of concurrency bugs. – erickson Sep 07 '12 at 01:06
  • Err.. why the continual thread start anyway? What's wrong with a while loop with a Wait at the top? When you want to kill the Aura, Notify() it. Am I missing something? – Martin James Sep 07 '12 at 06:39

2 Answers2

7

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.

Konrad Reiche
  • 27,743
  • 15
  • 106
  • 143
  • Wow, okay thanks this helps a lot... I'll have to find a new way to do this. And thanks wug, I'll try to do this with out a thread – user1653457 Sep 07 '12 at 01:03
5

You're right, threads can run only once and it's illegal to start/run a thread more than once. You should consider using a while loop to keep your thread alive.

Instead of directly dealing with Threads, you should be using the classes inside the java.util.concurrent package to schedule a fixed task at regular intervals which is apparently what you're trying to do. Take a look at ThreadPoolExecutor.

asgs
  • 3,928
  • 6
  • 39
  • 54
  • 1
    +1 for the while loop. A Wait() at the top of the loop should work - just Notify() it when you want to kill the Aura. – Martin James Sep 07 '12 at 06:40