• 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 ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

How to find system is idle

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
I want to make one small application which runs in background.But want it to perfrom few tasks when the system is idle.
Can anyone please tell me how to find system state in java, like if its idle or being used, so that my application tasks can be performed at that time.

thanks,
Mehul
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One option is to run your application with threads set at a very low priority. Modern JVM do integrate threads with the OS, so it should work -- just be careful to not starve your application.

However, if you are looking to see what the load is on the OS, the easiest way is to do a Runtime.exec() to get the data from the OS. You can do a "vmstat" for unix, and a "typeperf" for Windows.

Henry
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would you want the application itself to determine when it should run? This decision most appropriately made at the system level.

I advocate against using application threads to idle a program until nothing else is going on. It's an approach that trains the local context (the process) to react to the demands of the global context (the operating system). Since operating systems are inherently interrupt-driven -- it's their job to figure out when to run each process and for how long -- you're better off letting them do that work.

You may want to go about this your own way for other reasons, but I'd be leery of turning Java into a some kind of golden hammer.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On linux you could repeatedly read /proc/loadavg.
 
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:
.... and a "typeperf" for Windows.

Henry



I typed "typeperf" on Command Prompt and in Start-Run, but there is no such command in Windows 2000 Prof.
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chetan Parekh:


I typed "typeperf" on Command Prompt and in Start-Run, but there is no such command in Windows 2000 Prof.



Hey it is only there for Windows XP. Check this.
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michael Ernest:
I advocate against using application threads to idle a program until nothing else is going on. It's an approach that trains the local context (the process) to react to the demands of the global context (the operating system). Since operating systems are inherently interrupt-driven -- it's their job to figure out when to run each process and for how long -- you're better off letting them do that work.



I sort-of agree.

I don't think that an application should try to set itself to run all of its threads at a low priority. As you say, it's better for such an application to do nothing to set its own priority; the person starting the application can then choose whether to run it at normal or low priority.

However, I do think it's entirely valid for an application to run some of its threads at a lower priority. A multi-threaded application may have some tasks that it wants to run only when there are no more-important tasks to do. I've used this for clean-up tasks etc. One does have to be careful what is done in low-priority threads, particularly around claiming monitor locks and other contended resources.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Mehul Mistry
i have done something like this....


wrote program in c and called it from java using JNI
 
Michael Ernest
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Peter Chase:

However, I do think it's entirely valid for an application to run some of its threads at a lower priority.


It would be hard to disagree, since there is no alternative approach to managing multiple threads in the same process. However:

Let's take Solaris as an example. In Solaris, we have the notion of scheduling classes. Each class has its own range of priorities and its own rules for determining who runs next, for how long, how to assign time to processes of the same priority, and how to resolve issues such as starvation, CPU hogging, etc. One such problem is known as priority inversion: a condition that occurs when a low-priority grabs a lock, but it then pre-empted by a higher priority process that wants the lock. Most classes have the ability to raise and lower the priority of each process thread according to its own rules. In short, your thread assignments are, at best, advices to the system. They are not directives.

Solaris scheduling classes include timesharing (TS), interactive (IA), Fixed (FX), System (SYS), fair share scheduling (FSS), and real-time (RT). You can use them all at the same time if you're feeling pecking, so Solaris also has a scheme to rank the classes. That is, the classes all map into a global range of priorities which run according to the dictates of the collective conscience. Each class, when it gets time to run, decides which of its member processes will benefit and in what order.

Java threads can only assume a priority within the context of the JVM, which in turn is assigned whatever class it's assigned to, which in turn is relative to all the different classes running and the load they're carrying.

It's not likely that your everyday admin will plop your JVMs into real-time mode to cause havoc. Still, it's important to remember that JVM thread priority assignment is *always* relative its environment.
 
Mehul Mistry
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey thanks to everyone of you... Your suggestion and discussion has proved to be a lot of help.

regards,
Mehul
 
Of course, I found a very beautiful couch. Definitely. And this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic