• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Control CPU usage at different levels

Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Using Java, I have spent a lot of time trying to make the sleep(long milli, int nano) method to work. My main purpose is to try and control the CPU usage at different percentage levels.

I got this information from the link below under Threads, �A new form of the sleep() method is provided which allows for sleep times smaller than one millisecond.�

Link is:

Is it the same sleep method as above? I tried using sleep( 0, 1 ) with Windows XP Pro where I am expecting a CPU usage at or near 100% but its going only at 2%. When I change to sleep( 0 , 0 ) the CPU usage is at 100%.

I am using JBuilder Enterprise and Foundation with JDK 1.5.

If its not possible to make the sleep method work properly, is there another way to do it in Java?

Thanks for your help.
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is certainly not a beginner's question. It really belongs in the specialised multithreading forum...

Your understanding of how multithreading works seems to be fundamentally flawed.
You cannot control the running of a thread, and certainly not to the precision allowed in theory by the nanosecond precision option.

While in theory it COULD give you nanosecond precision over the minimum time the thread will sleep (NEVER over the exact time) in reality computers cannot (yet) handle such precision and will round to the nearest several milliseconds (most will use a minimum block of 20 msecs or so). As 1 nanosecond rounded to the nearest millisecond (let alone 20 of them) is 0 your thread will be sent to sleep for a period of no time at all.
If somebody says you look familiar, tell them you are in porn. Or in these tiny ads:
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic