Results 1 to 4 of 4
  1. #1
    Keesjuh is offline Member
    Join Date
    Jul 2013
    Posts
    8
    Rep Power
    0

    Default Button.setText failure when using thread.sleep

    Im trying to create a program which measures reaction speed. When clicked on a button, the program will show "Wait" for several (but variable) seconds and after that the text will be changed in "Press". The problem is that for some reason the button doesnt show the "Wait" message but does show the "Press" message. I find this very strange because without the Thread.sleep the button message is changed. Can anyone help?

    Java Code:
     public void countingDown() {
    		waitingTime = (long) (Math.random() * 1000 + 3000);
    		button.setText("Wait");
    		try {
    			Thread.sleep(waitingTime);
    		} catch (Exception e) {
    		}
    		time1 = System.currentTimeMillis();
    		button.setText("Press");

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default Re: Button.setText failure when using thread.sleep

    I think it`s because the countingDown method is invoked by the event dispatch thread(edt) (e.g. it`s calld from a listener method). the thread.sleep acts on the edt - the edt is responsible for drawing the gui/buttons....if the edt sleeps, it can`t draw :-)
    put
    Java Code:
    new Thread(new Runnable() {
    					@Override
    					public void run() {
    						countingDown();
    					}
    				}).start();
    to your listener code instead. But Swing is not thread safe ( javax.swing (Java Platform SE 6).)

    Edit: oh sorry it is android not Swing......
    Last edited by eRaaaa; 10-22-2013 at 08:54 PM.

  3. #3
    Keesjuh is offline Member
    Join Date
    Jul 2013
    Posts
    8
    Rep Power
    0

    Default Re: Button.setText failure when using thread.sleep

    I tried this, and after clicking the button does show the text "Wait", but after the seconds its supposed to wait it crashes.
    Can you also give some additional explanation to what im doing here exactly? I dont really understand :-)
    Thanks for helping!

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    5

    Default Re: Button.setText failure when using thread.sleep

    Calling this in the EDT, are you? Calling setText probably doesn't change the text directly. Rather, it queues a task in the EDT to change it. Then the thread sleeps. Then it runs the task that changes it to "Wait", followed immediately by the task that changes it to "Press". Then it repaints the screen, and you only see "Press".

    Don't use Thread.sleep(...) in the UI thread. Create a Handler and use Handler#postDelayed(Runnable, long) (if I remember the method signature correctly...)
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Replies: 1
    Last Post: 09-24-2012, 07:01 PM
  2. 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
  3. 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
  4. thread.sleep
    By jc5 in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-02-2009, 11:51 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
  •