Results 1 to 6 of 6

Thread: Thread vs Timer

  1. #1
    dawiz001 is offline Member
    Join Date
    Feb 2009
    Posts
    21
    Rep Power
    0

    Question Thread vs Timer

    I'm trying to drive and animation using a Thread. I can achieve this using a Timer, but the Thread is giving me a real prob. Any suggestions? Thanks.

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    What problems your experiencing using a Thread?
    I die a little on the inside...
    Every time I get shot.

  3. #3
    dawiz001 is offline Member
    Join Date
    Feb 2009
    Posts
    21
    Rep Power
    0

    Default

    Truth be told I don't even know where to begin?

  4. #4
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Try this ;)

    Java Code:
    public class ThreadExample implements Runnable
    {
        private final Thread thread;
        private final int TIME_IN_MILLIS = 1000; // 1000 ms = 1 sec
        private final int cycletime = TIME_IN_MILLIS;
    
        public static void main(String[] args) {
            new ThreadExample();
        }
    
        public ThreadExample() {
            thread = new Thread(this);
            thread.start();
        }
    
        private void process() {
            // Do stuff
        }
    
        public void run() {
            while (true) {
                process();
                try {
                    thread.sleep(cycletime);
                }
                catch (InterruptedException ie) {
                    System.out.println("Error: "+ie);
                }
            }
        }
    }
    I die a little on the inside...
    Every time I get shot.

  5. #5
    dawiz001 is offline Member
    Join Date
    Feb 2009
    Posts
    21
    Rep Power
    0

    Default

    Thanks a lot!

  6. #6
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    You're Welcome. :)
    I die a little on the inside...
    Every time I get shot.

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, 05:37 PM
  2. Inside a Timer thread loop,how to refresh a JTable in swing
    By neha_negi in forum Threads and Synchronization
    Replies: 3
    Last Post: 09-04-2009, 01:45 AM
  3. Replies: 0
    Last Post: 04-04-2008, 02:46 PM
  4. Replies: 0
    Last Post: 01-28-2008, 07:02 AM

Posting Permissions

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