Results 1 to 6 of 6
Thread: Thread question
- 02-09-2009, 07:13 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 92
- Rep Power
- 0
Thread question
Greetings,
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.Java Code:p1.run(); Thread.sleep(1000); p1.stop(); 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.
Any tips?
Thanks
- 02-09-2009, 07:42 PM #2
Member
- Join Date
- Jan 2009
- Posts
- 92
- Rep Power
- 0
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.
- 02-09-2009, 08:11 PM #3
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
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.Neil Coffey
Javamex - Java tutorials and performance info
- 02-09-2009, 08:53 PM #4
Member
- Join Date
- Jan 2009
- Posts
- 92
- Rep Power
- 0
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.
- 02-09-2009, 09:20 PM #5
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
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.Neil Coffey
Javamex - Java tutorials and performance info
- 02-09-2009, 10:33 PM #6
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.
Similar Threads
-
Difference between Thread.yield() and Thread.sleep() methods
By Nageswara Rao Mothukuri in forum New To JavaReplies: 12Last Post: 07-30-2010, 05:37 PM -
Thread.sleep() question
By Lachezar in forum New To JavaReplies: 5Last Post: 02-03-2009, 10:27 PM -
If JNI thread call the java object in another thread, it will crash.
By skaterxu in forum Advanced JavaReplies: 0Last Post: 01-28-2008, 07:02 AM -
main thread question?
By frejon26 in forum New To JavaReplies: 1Last Post: 01-24-2008, 10:13 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks