Results 1 to 6 of 6
- 04-18-2009, 06:54 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 96
- Rep Power
- 0
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.
- 04-18-2009, 07:41 AM #2
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
- 04-18-2009, 11:36 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 04-20-2009, 06:01 AM #4
Member
- Join Date
- Feb 2009
- Posts
- 96
- Rep Power
- 0
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."
- 04-20-2009, 06:31 AM #5
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
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.Neil Coffey
Javamex - Java tutorials and performance info
- 04-20-2009, 06:51 AM #6
Member
- Join Date
- Feb 2009
- Posts
- 96
- Rep Power
- 0
Similar Threads
-
Difference between Thread.yield() and Thread.sleep() methods
By Nageswara Rao Mothukuri in forum New To JavaReplies: 12Last Post: 07-30-2010, 05:37 PM -
an old question about Thread.Sleep()
By narbeh in forum Threads and SynchronizationReplies: 1Last Post: 03-25-2009, 07:12 AM -
Thread.sleep() question
By Lachezar in forum New To JavaReplies: 5Last Post: 02-03-2009, 10:27 PM -
Sleep in thread
By jithan in forum New To JavaReplies: 1Last Post: 08-27-2008, 02:27 PM -
Can't get my thread to sleep!
By jamesfrize in forum New To JavaReplies: 2Last Post: 03-25-2008, 05:14 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks