Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting a priority

 
Christopher Young
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bob Good
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Christopher Young
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic