This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori 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

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Setting a priority" Watch "Setting a priority" New topic

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?

or perhaps this:
[ March 18, 2008: Message edited by: Bob Good ]
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42958
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?
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:

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 -
if (Posts < 30) JavaRanchFAQ);
Christopher Young
Ranch Hand

Joined: Nov 02, 2007
Posts: 63

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:
subject: Setting a priority
It's not a secret anymore!