fork download
  1. import java.util.*;
  2.  
  3. class Ideone {
  4.  
  5. public static class Sprite {
  6. public static List<Sprite> sprites = null;
  7. public int age = 0;
  8.  
  9. private boolean removed = false;
  10.  
  11. public Sprite() {
  12. sprites.add(this);
  13. }
  14.  
  15. public boolean render() {
  16. if (removed)
  17. return false;
  18. age++;
  19. if (age > 30)
  20. dispose();
  21. return true;
  22. }
  23.  
  24. public void dispose() {
  25. removed = true;
  26. }
  27. }
  28.  
  29. private static List<Sprite> sprites = Collections.synchronizedList(new ArrayList<Sprite>());
  30.  
  31. public static void main(String[] args) {
  32. Sprite.sprites = sprites;
  33.  
  34. new Sprite();
  35. new Sprite();
  36.  
  37. for (int i = 0; i < 60; i++)
  38. render();
  39.  
  40. }
  41.  
  42. public static void render() {
  43. synchronized(sprites) {
  44. Iterator<Sprite> iterator = sprites.iterator();
  45. while (iterator.hasNext())
  46. if (!iterator.next().render())
  47. iterator.remove();
  48. }
  49. }
  50. }
  51.  
Success #stdin #stdout 0.09s 320320KB
stdin
Standard input is empty
stdout
Standard output is empty