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

    Default confusion with methods sleep() and join()

    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

    Please don't cross-post.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

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

    Default

    ok from now onwards i will never post same thread in two catageries.

  4. #4
    Mrs. Deswal's Avatar
    Mrs. Deswal is offline Member
    Join Date
    Apr 2009
    Location
    Gurgaon,India
    Posts
    24
    Rep Power
    0

    Default

    hi,

    You already know about sleep() method. This method doesn't invoke any other thread, this only causes the thread to wait for the time you have pass as arguments. In join() method the main thread wait for the thread to finish its task and then destroy that thread for which join() method is called.
    Sharing knowledge means gaining more knowledge.:)

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

    Default

    If you make one thread sleep, then yes, this generally gives other threads "more opportunity" to run. So indirectly, yes, sleeping one thread makes other threads run. However:

    - you have little control over which threads will run instead when you put a given thread to sleep;
    - sleep or no sleep, other threads will still get a share of the CPU.

    You certainly shouldn't see sleep() as a means of controlling other threads-- that's really not what it's for.

    join() makes the current thread waits for the given thread's run() method to finish. But calling join() doesn't actually do anything to that other thread (it certainly doesn't destroy it-- that's something that just "happens naturally" some time after that thread's run() method finishes).

    If you do need to make one thread control another, look at CountDownLatch, possibly the wait/notify mechanism (though it's a little deprecated nowadays), and other classes in the java.util.concurrent package (which have generally replaced wait/notify).

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() and join() question
    By sandeepsai39 in forum New To Java
    Replies: 5
    Last Post: 04-20-2009, 06:51 AM
  3. Regarding Join
    By adeeb in forum JDBC
    Replies: 1
    Last Post: 06-24-2008, 07:57 PM
  4. How to use join() method
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:33 PM
  5. i'm happy to join this forum
    By sivasayanth in forum Introductions
    Replies: 1
    Last Post: 12-25-2007, 08: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
  •