Results 1 to 4 of 4
  1. #1
    atomant is offline Member
    Join Date
    May 2010
    Posts
    45
    Rep Power
    0

    Default Trouble with Thread.sleep

    I have some code that I am having trouble with. I want it to simply display the text "ONE", pause 3 seconds, then display "TWO". What is happening is it pauses 3 seconds then displays "TWO", without displaying "ONE". I was having this problem in a program I am running so I made this simplified example and it is still happening.

    I just tried it and this is only happening when I use init(), but when I use run() it works. Is there a way to get it to work with init()?

    Java Code:
    import acm.graphics.*;
    import acm.program.*;
    
    public class Testing extends GraphicsProgram {
    	
    	public void init() {
    		GLabel label = new GLabel("ONE");
    		label.setLocation(100,100);
    		add(label);
    		try {
    			Thread.sleep(3000);
    			} catch(InterruptedException e) {
    			} 
    		label.setLabel("TWO");
    	}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,254
    Rep Power
    19

    Default Re: Trouble with Thread.sleep

    I haven't a clue about the GraphicsProgram class that you extend, but if it's anything like Swing then you need to go through this Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default Re: Trouble with Thread.sleep

    Quote Originally Posted by DarrylBurke View Post
    I haven't a clue about the GraphicsProgram class that you extend, but if it's anything like Swing then you need to go through this Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    I haven't much of a clue either but I vaguely remember that those Gxxx objects are built on top of Swing objects so setting the text of the label and the sleeping needs to be done in a separate thread.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Trouble with Thread.sleep

    Yeah, I think Jos is correct - you can try using a timer object to perform the changes!

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. how to reduce the thread sleep time and wake up the thread
    By baktha.thalapathy in forum Threads and Synchronization
    Replies: 2
    Last Post: 06-24-2010, 07:36 PM
  3. thread.sleep
    By jc5 in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-02-2009, 11:51 PM
  4. Sleep in thread
    By jithan in forum New To Java
    Replies: 1
    Last Post: 08-27-2008, 02:27 PM
  5. How to use the sleep and thread?
    By jiuhu in forum Java Applets
    Replies: 4
    Last Post: 08-07-2007, 02:56 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
  •