This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Setting a priority Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Setting a priority" Watch "Setting a priority" New topic
Author

Setting a priority

Christopher Young
Ranch Hand

Joined: Nov 02, 2007
Posts: 63
Is there a way in code to set the priority in which a program should run at? I looked in the system class and couldn't see anything but am not sure where else to look.

I have a small server program that is in an infinite loop to listen for connections along a certain port. It's for a chat thing I'm developing, but I know it doesn't need to be taking up 100% CPU time for this one little task (I have a few threads going so that it can process input and output and I start new threads every time for a client to talk). everything is working well for it so I'm just wondering if there's a way to tune down its priority if I want to run it as a more permanent server and use this computer for other stuff.

I know I can just go into the taskmanager and change it that way, just wondering if there is a way in code to do this.


Technology can never substitute for knowledge.
Bob Good
Ranch Hand

Joined: Jan 09, 2008
Posts: 86
Does this help?

http://www.science.uva.nl/ict/ossdocs/java/tutorial/java/threads/priority.html

or perhaps this:

http://forum.java.sun.com/thread.jspa?threadID=647099&messageID=3809986
[ March 18, 2008: Message edited by: Bob Good ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42047
    
  64
I think those two articles slightly miss the point, as they're concerned with Java's thread priorities, but what's at stake here is the priority of the JVM process as a whole within the OS.

When I hear about a loop using 100% CPU time, the first thing I'm thinking is that there's active waiting going on. How about putting a "Thread.sleep(100)" in each loop iteration? Or is that too long for the server to wait between iterations?


Ping & DNS - my free Android networking tools app
Bob Good
Ranch Hand

Joined: Jan 09, 2008
Posts: 86
There was a lot of discussion of windows thread and processes versus java thread priorities in the javaworld article pointed to in the sun blog:

http://www.javaworld.com/javaworld/jw-09-1998/jw-09-threads.html

It also discusses differences on Solaris etc.
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
Since process priority is OS specific, and Java tries to be OS agnostic, I would think you need to solve this problem separately for each OS you are on.


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Christopher Young
Ranch Hand

Joined: Nov 02, 2007
Posts: 63
Hmm.

I'll look into it.

I am not running the server's listening command off a thread but I can easily do that. But yeah, I am looking at it at the OS level rather than at the thread.
[ March 18, 2008: Message edited by: Christopher Young ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Setting a priority