Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7022995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:39:14+00:00 2026-05-27T23:39:14+00:00

I wrote a daemon which was structured like this: while( true ) { //

  • 0

I wrote a daemon which was structured like this:

while( true ) {
  // do some stuff
  Thread.sleep( 1000 );
}

I noticed it was using a very large amount of CPU – up to 100%. I have had a similar daemon on my production servers for some months with the same CPU problem.

Yesterday I refactored the code to use TimerTask. Immediately I noticed that CPU usage had decreased on my dev box. So I decided to deploy to production and double-check using Munin. Here are the graphs:

Load average

CPU usage

A couple of points:

  • There is absolutely nothing else running on the production server except the JVM.
  • There are no other application threads running
  • It was definitely executing the old-style code at the correct periodic intervals – I always write to the log each time the thread executes.

So: why is Thread.sleep so inefficient compared to TimerTask?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:39:15+00:00Added an answer on May 27, 2026 at 11:39 pm

    Three possibilities I can think of:

    • You have a huge number of threads doing this, and they’re context switching all the time. Using a timer will mean there’s only one thread instead. On the other hand, that means you will only get one task executing at a time.
    • You have a continue; statement somewhere in your loop before the sleep, so even if the main body of work of the loop isn’t executing very frequently, something is. It’s hard to say without seeing some more concrete code though.
    • You have a broken JVM/OS combination. This seems pretty unlikely, admittedly.

    A simple loop just executing Thread.sleep(1000) repeatedly should be very cheap – and that should be easy for you to verify, too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write app, which will work like a daemon and on some
I wrote a windows service using VB that read some legacy data from Visual
I wrote scripts which permit to synchronise some of my local folders with a
I wrote this script to find all files/directories to which $WWWUSER has write permissions.
I wrote a simple daemon. This daemon should respond when I run any program.
I'm using a daemon-script which is monitoring a remote server. When the remote server
I like to know how to write a program which would export some API
I've bumped into a strange problem. I wrote a little daemon in Perl which
How to write a shell script/process which runs like a daemon on Unix and
I just wrote php daemon for my app and want to implement some output

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.