Results 1 to 6 of 6

Thread: Thread question

  1. #1
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Question 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:

    Java Code:
    p1.run();
    Thread.sleep(1000);
    p1.stop();
    System.out.println("Thread waited too long");
    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.

    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

  2. #2
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default

    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.

  3. #3
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    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.

  4. #4
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default

    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.

  5. #5
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    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.

  6. #6
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    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

  1. Difference between Thread.yield() and Thread.sleep() methods
    By Nageswara Rao Mothukuri in forum New To Java
    Replies: 12
    Last Post: 07-30-2010, 06:37 PM
  2. Thread.sleep() question
    By Lachezar in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 11:27 PM
  3. Replies: 0
    Last Post: 01-28-2008, 08:02 AM
  4. main thread question?
    By frejon26 in forum New To Java
    Replies: 1
    Last Post: 01-24-2008, 11:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •