Not hogging the processor in Java

Started by
10 comments, last by Drigovas 15 years, 5 months ago
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?
Advertisement
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.
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.
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?
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.
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.
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.
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.
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)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
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.
NetGore - Open source multiplayer RPG engine

This topic is closed to new replies.

Advertisement