• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

javax.swing.timer taking up too much cpu (mac)

 
Ash Whin
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys

I made this minigame for project (with a lot of stolen gifs lol, ill correct that later) and it works pretty well... except for its incredible cpu usage of 50%, even though the graphics are 8 bit and obviously dont take up a lot of graphics painting space... How the code works is pretty simple:



                       

The code and is attached (not the game because zip files are not allowed). I'm not sure what the problem is, is it the huge amount of arraylist for loops that might be lagging out the cpu? the duration of the timer has to be 5 otherwise the physics would be glitchy so I can't change that.

Also this is the mac version so sorry windows users :P I'm making the windows version later after I fix the copyright
 
Ash Whin
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Paul Clapham
Sheriff
Posts: 21551
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ash Whin wrote:I'm not sure what the problem is, is it the huge amount of arraylist for loops that might be lagging out the cpu? the duration of the timer has to be 5 otherwise the physics would be glitchy so I can't change that.


That's 5 milliseconds per whatever it is you're doing repeatedly. Or 200 times per second if you want to look at it another way. I'd try doing the repetition a lot less frequently until you get a number where the performance and the physics work satisfactorily.
 
Ash Whin
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I edited the thread type and now the cpu usage is at 20%. Much better but not good enough for an 8 bit game :P How can i get it to around 10% and below?

 
Rob Spoor
Sheriff
Pie
Posts: 20745
68
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't do anything to the UI in any other thread. See Concurrency in Swing for more information. Also, you know that you're sleeping one millisecond each time there, right? That's basically the same as not sleeping at all.

Did you read Paul's comment on the number of repaints per second? 200 is much too high. Trying to get 60 is a lot more realistic. That would give you a delay of 16ms instead of 5.
 
Ash Whin
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried using some concurrency but it didn't do too much unfortunately, its stuck at 25% CPU with the following code SwingWorker and ActionListener combo:



I'm not getting this very well, can someone give me an explanation on what I'm doing wrong?
 
Ash Whin
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Sheriff
Posts: 51341
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:. . . That's 5 milliseconds per whatever . . .
It is a long time since I tried any such timings on Swing, but when I did, I couldn't refresh a display in less than about 20ms.
 
Piet Souris
Rancher
Pie
Posts: 1513
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is a difference between updating your game and displaying it. The updating can be done as many times per second as you like, or is feasable, while displaying could be done in another tempo, maybe every three to four updates. To get a decent balance between the two, you should do some measurements how long an update takes. Have you considered some hardware accelleration like JavaFX or OpenGL?
 
Rob Camick
Ranch Hand
Posts: 2668
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thread.sleep(1); //Without this CPU usage is 150% What is going on here?



You should not be using a while true loop. This will cause the infinite loop which will hog the CPU.

You either need to either:

1) use a Timer to schedule some kind of animation or
2) have an end condition for the loop to stop.
 
Campbell Ritchie
Sheriff
Posts: 51341
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are all sorts of confusing things about your code, apart from why are you writing while (true) ...?
Ash Whin wrote:
Those aren't doubles on lines 15‑16; they are ints. Why are you mixing doubles and floats? Why are you using floats at all? Just because line 20 compiles and runs, that doesn't make it is correctly typed. What does that bit about nanoTime mean in line 19? Why have you got a loop to reduce delta below 1? What is wrong with the % operator? Why are you trying to deal with sizes < 1?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic