Jump to content
  • Advertisement
Sign in to follow this  
hackers238

Not hogging the processor in Java

This topic is 3553 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!