Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Problem with thread.sleep()

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Problem with thread.sleep()

    ok, so im making a program that you can do ascii animations with. So i have to play the movie frame by frame. So i want my code to play one frame, Sleep 1 sec,play frame 2, sleep 2 sec etc. What it does instead is waits 10 secs, then plays frame 9. Anyone know what im doing wrong?

    for(location=0;location<10;location++)
    {
    	movie.setText(frames[location]);
    	try 
    	{
    	Thread.sleep(1000);
    	}
    	catch (InterruptedException e) 
    	{
    	e.printStackTrace();
    	}						
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Problem with thread.sleep()

    In swing all painting takes place on the same thread. So when you sleep that thread, none of the other frames are able to re-paint themselves.

    What I would recommend doing is to remove the Thread.sleep and instead use a swing timer which would fire every 1 second, triggering the update.

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem with thread.sleep()

    How do i implement that im trying this and it doesnt work.

    public void actionPerformed(ActionEvent event)
    {
    	for(location=0;location<3;location++)
    	{
    		movie.setText(frames[location]);;
    		int pause = 1000;
    		timer = new Timer(pause, this);
    		timer.start();
    	}
     
     
     
    }

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Problem with thread.sleep()

    Creating the same timer 3 times through the loop (an infinite number of times overall). That's not exactly an ideal situation. Create it once (outside of the ActionPerformed method), and set it to repeat. Each time the event fires, update your text once. On the tenth time through (keep an external counter, initialize it to 0 where you create your timer), you can stop the timer.

  5. The Following User Says Thank You to helloworld922 For This Useful Post:

    stormforce (April 19th, 2011)

Similar Threads

  1. How to convert thread.sleep() into a timer in an Applet
    By all_pro in forum Java Theory & Questions
    Replies: 2
    Last Post: April 14th, 2011, 07:45 AM
  2. [SOLVED] Giving Thread.sleep( ); a decimal value
    By Knox in forum Threads
    Replies: 2
    Last Post: April 9th, 2011, 08:55 PM
  3. [SOLVED] Thread.sleep() in while loop not working
    By mds1256 in forum Threads
    Replies: 4
    Last Post: January 13th, 2011, 05:02 AM
  4. Thread Sleep, Timer, Button Question
    By tabutcher in forum Java Theory & Questions
    Replies: 1
    Last Post: May 1st, 2010, 02:54 AM
  5. Thread/timer problem
    By korbal in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 18th, 2010, 05:59 PM