?
Solved

sleep() versus wait(obj) performance

Posted on 2003-11-18
8
Medium Priority
?
348 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 15

Accepted Solution

by:
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.
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

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

 

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
 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

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

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month14 days, 2 hours left to enroll

800 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