Solved

sleep() versus wait(obj) performance

Posted on 2003-11-18
8
319 Views
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.
0
Comment
Question by:jkelly061597
8 Comments
 
LVL 15

Accepted Solution

by:
jimmack earned 100 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.
0
 
LVL 16

Expert Comment

by:imladris
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.
0
 

Author Comment

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

Author Comment

by:jkelly061597
ID: 9773046
Possible question: Are they implemented in the JVM in a way that makes one worse off than the other?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Expert Comment

by:doronb
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.
0
 
LVL 15

Expert Comment

by:JakobA
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.
0
 
LVL 15

Expert Comment

by:jimmack
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.
0
 

Author Comment

by:jkelly061597
ID: 9780451
Thanks everyone,

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
base64 decode encode 12 121
Image decoding from Camera 3 72
ForLoop Example 3 40
javap not working 8 37
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

920 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now