Results 1 to 6 of 6
  1. #1
    sandeepsai39 is offline Member
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    0

    Default Thread.sleep() and join() question

    hi to all,iam new to java threads iknow sleep() method causes current thread to pause execution for specific time,but when we call sleep() method on current thread will it cause the other threads to start their process that are waiting.One more question what exactly join() method do .Ialready read the sun java tutorials ,but i am in little bit confusion.please help me.

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    If one thread is sleeping then other threads are free to be executed.

    Thread.join() causes the current thread to sleep until the joined thread has terminated.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    In other words, let say I want to use multiple threads to do a task. And also I want to do the same task continuously, or several times. But until finish all the threads, I cannot start the task for the next time. What should I do?

    I want to say to the main thread, wait until all other threads are complete there execution. Thread.join() comes to the picture in that case. Depends on the implementation, there are three overloaded methods to use.

  4. #4
    sandeepsai39 is offline Member
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    0

    Default

    Your explanation is very nice i got your points except last line i.e
    "Depends on the implementation, there are three overloaded methods to use."

  5. #5
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    What Eranga means is that with Thread.join(), you have the option of giving a timeout. In effect, you say "wait till this thread finishes, or for a maximum of X milliseconds, depending on which occurs soonest". You can also specify a timeout in nanoseconds, but no current OS can honour that level of granularity.

    Calling sleep() doesn't directly cause any other thread to take any specific action. It basically says to the OS "I don't need any processor time for the next X milliseconds". So typically, the OS will deschedule the thread (not actually give it any processor time) for roughly that time. As an indirect consequence, some other thread(s) probably will then get the processor.

    But if you don't sleep, then eventually your thread will be kicked off the processor anyway to let other threads get a chance. Different OS's have a policy of how they handle this.

    If you're interested in more information, I've written various articles (some slightly advanced) on these topics that I'd recommend you take a look at (well, I'm biased, but I think they're interesting...):

    • Thread.sleep(): behaviour and performance - in particular, I include some measurements of what Thread.sleep() actually does from a performance/scheduling point of view under various levels of load
    • Thread scheduling: an overview of how various OS's decide "which thread gets the processor next", which obviously ties into what Thread.sleep() does.


    Have a poke around at the links at the top too -- there are various related articles that might interest you. For example, you might want to look at the thread priorities info.

  6. #6
    sandeepsai39 is offline Member
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    0

    Default

    thank u neil.I got your points.Thank u for suggesting good articles.First i will learn threads at basic level ,and then i come back with your articles .

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, 06:37 PM
  2. an old question about Thread.Sleep()
    By narbeh in forum Threads and Synchronization
    Replies: 1
    Last Post: 03-25-2009, 08:12 AM
  3. Thread.sleep() question
    By Lachezar in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 11:27 PM
  4. Sleep in thread
    By jithan in forum New To Java
    Replies: 1
    Last Post: 08-27-2008, 03:27 PM
  5. Can't get my thread to sleep!
    By jamesfrize in forum New To Java
    Replies: 2
    Last Post: 03-25-2008, 06:14 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
  •