Results 1 to 8 of 8
  1. #1
    jc5
    jc5 is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default calling thread.sleep stalls main gui

    Hi,

    When I call thread.sleep in another thread, it seems to stall even the main event thread (the gui doesn't update itself for a long period of time). I was wondering if calling thread.sleep influences any of the other threads that may be running at the same time?

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    It could. If you call a Thread.sleep when handling a button click, for example, it will freeze that button's UI for as long as you sleep.

    If you have a sleep within the run method of a thread then i can't see why it would effect other threads unless they are deadlock or competing for a shared resource that you have locked.
    My Hobby Project: LegacyClone

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Agrees with MrMatt above. If however you want a more specific example then please post some sample code that demonstrates your problem.

    Edit: also, if this is a Swing program, please have a look at this tutorial: Concurrency in Swing

    Much luck and welcome to the forum.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    From your description that is the behavior you would get if you do call sleep on the EDT.
    Either you are mistakingly calling sleep on the EDT or you are running some long task on the EDT.
    Last edited by r035198x; 09-03-2009 at 08:27 AM.

  5. #5
    Jitu2000 is offline Member
    Join Date
    Sep 2009
    Posts
    1
    Rep Power
    0

    Default This is correct and expected behavior

    The thread that invokes the sleep command is the one that is put to sleep.

    It does not matter on what thread u invoked the sleep method, always the calling thread goes to sleep.

    Therefore, in your case the main thread itself is going to sleep.

  6. #6
    alchemist is offline Member
    Join Date
    Sep 2009
    Posts
    6
    Rep Power
    0

    Default

    For all intensive purposes the main thread (you only have one) is in fact the thread the UI is operating on. Calling Thread.sleep will sleep the current thread, hence the UI locking up. If you need to have independent operations taking place then you need to run them on a different thread.

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by alchemist View Post
    For all intensive purposes the main thread (you only have one) is in fact the thread the UI is operating on. ...
    The main thread and the EDT are different threads.

  8. #8
    alchemist is offline Member
    Join Date
    Sep 2009
    Posts
    6
    Rep Power
    0

    Default

    Sorry my mistake :)
    The event dispatch framework however can to someone new appear to be part of the main thread.

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. thread.sleep
    By jc5 in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-02-2009, 11:51 PM
  3. Sleep in thread
    By jithan in forum New To Java
    Replies: 1
    Last Post: 08-27-2008, 02:27 PM
  4. Can't get my thread to sleep!
    By jamesfrize in forum New To Java
    Replies: 2
    Last Post: 03-25-2008, 05:14 AM
  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
  •