I'm trying to do the following:
invoke the run method of a Thread
Put the current thread to sleep for 1 second
when the current thread wakes up, kill the thread regardless if its finished or not.
Heres my code I've tried to do the following:
p1. is a thread I've started in a new class. If this thread takes more than 1 second to run, i want to kill it, therefore I put the p1.stop() after Thread.sleep.
System.out.println("Thread waited too long");
The way i've tested this and know it does not work is inside the p1 thread i've put it to sleep for 10 seconds, meaning it should be stopped well before it did anything, yet what happens is, p1 sleeps for 10 seconds, wakes up and does its job, then the print statement is executed, well after the 1 second timer I expected it to.
I think I've found my solution. When a thread dies you can not use the thread anymore until you re-create the thread. And I believe you can use the join() method and Thread.Sleep() in order to create the situation I need. Any other thoughts would be great still.
If you're directly calling the run() method of a thread, then in general, alarm bells should be ringing. The way to make a thread run is to call its start() method; the system will then at its leisure call the thread's run() method, inside the newly running thread.
Then, to make a thread stop, you actually shouldn't call its stop() method, which is an inherently unsafe, deprecated method. You need to write your thread in such a way that you cleanly provide it with a stop mechanism. For some ideas, have a look at the articles I've written on how to stop a thread in Java and thread interruption.
Yup that explained a lot. My next leap is going to be trying to slow down a thread which I'm thinking is impossible right now.
I'm currently updating a GUI based on what that thread in the background is doing, but the thread executes its operations way faster than the human eye can see on. So if a square is to move 5 spaces, it instantly moves 5 spaces and you don't see the individual spots. I'd like to interupt the thread every X miliseconds and put it to sleep for X amount of seconds so that the GUI can update what it has done.
Hmm.... if the operation is happening so fast that the human eye can't detect it, is there really much point slowing it down just for the sake of a pretty GUI effect...? If you really do want to slow it down, then periodically make it sleep (call Thread.sleep()). N.B. Never sleep in the GUI thread: always make sure it's inside the run() method of a thread that you've created that you're sleeping.
If you have a large number of operations each which is lighteningly fast, then you have the option of only updating the GUI every so many operations, to give the user chance to read the progress, but without slowing the actual operation down.
I found the javax.swing.Timer (*not* java.util.Timer) class to be useful for animations. You set how often you want the timer to signal an event, and the listener is invoked on the EDT, so you can update the GUI directly. Using Timer, you don't have to create a thread, deal with Thread.sleep(), etc.