Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to find system is idle  RSS feed

 
Mehul Mistry
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Sheriff
Posts: 23292
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On linux you could repeatedly read /proc/loadavg.
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • 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.
 
srilatha kareddy
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!