Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


sleep() versus wait(obj) performance

Posted on 2003-11-18
Medium Priority
Last Modified: 2008-02-26
I have a multithreaded app where threads pause for a certain amounts of time. In some cases I also wake them up part way through the process.

Is using Object.wait() a more expensive operation than Thread.sleep() when using them for a threading pause? I.e. memory or processing costs... etc.
Question by:jkelly061597
LVL 15

Accepted Solution

jimmack earned 300 total points
ID: 9771960
If you want to pause for a certain amount of time (as you say), then you should use Thread.sleep(), since that has the ability to provide the amount of time to wait (depending on how accurate you want it).

In both cases, processing costs are approximately the same because the thread is taken out of the execution queue.  I'm not sure of the differences with regard to memory usage.
LVL 16

Expert Comment

ID: 9772371
sleep will just pause for the speified time.
wait allows you to pause, and get restarted when some other thread "notifies" that the situation may have changed. Depending on your situation, the latter may be more efficient.

Author Comment

ID: 9772612
I know the abilities of each. I am looking for performance information.

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Author Comment

ID: 9773046
Possible question: Are they implemented in the JVM in a way that makes one worse off than the other?

Expert Comment

ID: 9773449
I suspect that both Wait and Sleep behave the same way about counting the time before they resume control of the Thread.  However, since Java isn't meant to be a real-time langauge, I suppose there could be variances between different JVM implementations and also about how each method is naitivly implemented within the same JVM.  Best bet about getting performance information is to conduct tests on your own with a program that performs calls on both Wait and Sleep and logs the data.
LVL 15

Expert Comment

ID: 9774874
neithe sleep nor wait 'count time'  they just hang themself up in a que designed for processes 'waiting for a time interrupt' after telling the system when they want the interrup.

wait has some slightly more complex admin-chores to do because it can also be reactivated by a notify.

The difference is miniscule though. If your appication is so timecritical that it matters you should probably be using C or C++ instead.
LVL 15

Expert Comment

ID: 9777352
>> If your appication is so timecritical that it matters you should probably be using C or C++ instead

Just what I was thinking ;-)

Also, if the general principle is that the pause should occur for a specific amount of time, you'll need extra code (eg. a Timer) in order to manage the wait/notify anyway.

Author Comment

ID: 9780451
Thanks everyone,

Just FYI: This is not a time critical app, but a performance critical app so I'm just checking.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month15 days, 21 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question