Sign in to follow this  
hackers238

Not hogging the processor in Java

Recommended Posts

Hey I have a working chess program (which beats me :(). The biggest problem is while the computer is thinking right after I move, the whole computer freezes up to the point where I can barely move the mouse. If I turn this into an applet, it could be a disaster. Is there a way to limit the processing power my program gets?

Share this post


Link to post
Share on other sites
Reduce the branching factor of chess' state space?
Just kidding.

Could you just add a short sleep somewhere? Alternatively, there is a Thread.currentThread() method that gets you a reference to the currently executing thread, and you could possibly use setPriority to reduce its priority. Not sure if that would work though; it's just a thought.

Share this post


Link to post
Share on other sites
I think you need to move the computer thinking code to a different thread. SwingWorker[http://java.sun.com/developer/technicalArticles/javase/swingworker/] might be your best friend for this job.

Share this post


Link to post
Share on other sites
Quote:
Original post by vikashparida
I think you need to move the computer thinking code to a different thread. SwingWorker[http://java.sun.com/developer/technicalArticles/javase/swingworker/] might be your best friend for this job.


All that would do is cause another thread to hog the CPU.

In your computational loop, put a Sleep(0) or Sleep(1) at some intervals. This problem should also not exist on multi-core CPUs.

But I really can't remember seeing this problem with Java in a very long time. Which VM are you using and what hardware are you testing this on?

Share this post


Link to post
Share on other sites
Have you perchance been messing with the thread priorities? Generally it's a good idea to keep your priorities equal or below to Thread.NORM_PRIORITY.

Share this post


Link to post
Share on other sites
Quote:
Original post by hackers238
Hey

I have a working chess program (which beats me :(). The biggest problem is while the computer is thinking right after I move, the whole computer freezes up to the point where I can barely move the mouse.


Doing work in the rendering thread kills performance of other applications. Basically you are doing CPU intensive task somewhere you shouldn't.



Antheus, IIRC sleep(0) means the thread would sleep permanently. sleep(a,b) is the way to go.

Share this post


Link to post
Share on other sites
Quote:
Original post by Raghar
Doing work in the rendering thread kills performance of other applications. Basically you are doing CPU intensive task somewhere you shouldn't.

That doesn't even make sense.

Share this post


Link to post
Share on other sites
I am almost absolutely sure that just by moving your AI code to a different thread will solve the issue. Let your current thread handle mouse input, system events, and such.

Share this post


Link to post
Share on other sites
Quote:
Original post by Raghar
Quote:
Original post by hackers238
Hey

I have a working chess program (which beats me :(). The biggest problem is while the computer is thinking right after I move, the whole computer freezes up to the point where I can barely move the mouse.


Doing work in the rendering thread kills performance of other applications. Basically you are doing CPU intensive task somewhere you shouldn't.



Antheus, IIRC sleep(0) means the thread would sleep permanently. sleep(a,b) is the way to go.


Since he is using Java he should look at:
http://java.sun.com/javase/6/docs/api/java/lang/Thread.html

Thread.sleep(0) should sleep for a very short time, AFAIK only a buggy version of libgcj managed to sleep indefinitly when 0 ms was specified.

Thread.sleep(a,b) is essentially worthless on most desktop platforms since the timers don't have a high enough resolution for it to matter.

That said however Thread.sleep(0) is probably not a good idea, it seems as if some implementations simply ignore that command which means that Thread.yield() or Thread.sleep(1) are more appropriate (depending on what you need).

Ofcourse, with proper thread priorities set you don't really need to sleep anyway (The OS should make sure that other processes get enough cpu time then)

Share this post


Link to post
Share on other sites
Quote:
Original post by alnite
I am almost absolutely sure that just by moving your AI code to a different thread will solve the issue. Let your current thread handle mouse input, system events, and such.


The problem with that, though, is that he said it freezes the whole computer up, not just the program, which is also why it sounds like it could have an increased priority. Only other thing I can think of that would be freezing up the whole computer would be if it consumed a ton of memory and started forcing other applications / services into virtual memory.

Share this post


Link to post
Share on other sites
Quote:
Original post by Spodi
Quote:
Original post by alnite
I am almost absolutely sure that just by moving your AI code to a different thread will solve the issue. Let your current thread handle mouse input, system events, and such.


The problem with that, though, is that he said it freezes the whole computer up, not just the program, which is also why it sounds like it could have an increased priority. Only other thing I can think of that would be freezing up the whole computer would be if it consumed a ton of memory and started forcing other applications / services into virtual memory.


My assumption is that his program is single-threaded. If his program is single threaded then it makes sense why it's hogging the CPU because the control is not returned to the system (in this case JVM). If he puts his AI code into a separate thread, the original thread can pass the control back to the JVM while waiting for the AI thread to finish.

Share this post


Link to post
Share on other sites
That doesn't make any sense though. The system itself will preempt whatever thread he is running from and distribute time to other threads, unless there is something fishy going on with priority or virtual memory as has already been mentioned. The point is, something is suffocating the system, and it isn't a single Java thread because the system will preempt it frequently enough to not exhibit the symptoms described [like the mouse being unresponsive]. Something else is the culprit. Even an infinite loop would not result in what is being observed here.

Say...

When you are running your program, can you hear your hard drive being very active? [a symptom of virtual memory use] Have you tampered with thread priority? Are you spawning a lot of worker threads?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this