Solved

sleep() versus wait(obj) performance

Posted on 2003-11-18
8
310 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

758 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