Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (292)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Single Thread Scheduled Executor For Processing  (Read 484 times)
0 Members and 1 Guest are viewing this topic.
Offline Dane

Senior Newbie





« Posted 2013-02-27 22:28:48 »

I was wondering what your opinions are of this:

 Grin

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

public class Game implements Runnable
{

   private ScheduledExecutorService service;
   private boolean running;

   public Game()
   {
      this.service = Executors.newSingleThreadScheduledExecutor();
      this.start();
   }

   public void start() {
      if (!running) {
         this.service.submit(this);
      }
   }

   public void stop() {
      this.running = false;
   }

   @Override
   public void run() {
      try {

         // Do stuff here.

         // Sleep
        Thread.sleep(50);

         // If we're still active, re-submit.
        if (this.running) {
            this.service.submit(this);
         }

      } catch (Exception e) {
         e.printStackTrace();
      }
   }

}

Doing it wrong since 0x7CC.
Offline Danny02

JGO Knight


Medals: 36



« Reply #1 - Posted 2013-02-27 22:46:06 »

what is wrong with a simple while loop^^
this is just overkill
Offline sproingie
« Reply #2 - Posted 2013-02-28 00:29:54 »

Much as I like j.u.c and think everyone should use Executors ... I have to agree, how's this superior to a plain loop?
Games published by our own members! Check 'em out!
Try the Free Demo of Droid Assault
Offline Dane

Senior Newbie





« Reply #3 - Posted 2013-02-28 00:41:55 »

Well actually I never said this was superior to anything, I just asked for your opinions of it.  Grin

Doing it wrong since 0x7CC.
Offline sproingie
« Reply #4 - Posted 2013-02-28 01:19:09 »

I suppose you could say it's properly tail recursive by way of using the executor as a trampoline, though with no proper way to take parameters or return output, that's not a very interesting property, just a very expensive loop.  If the executor were shared with other Runnables using it you'd have a poor man's cooperative scheduler, emphasis on "poor".   Check out the green threads post for a heavy duty version of the same.

Online matheus23

JGO Wizard


Medals: 72
Projects: 3


You think about my Avatar right now!


« Reply #5 - Posted 2013-02-28 15:16:09 »

I'm pretty sure this is much slower than a simply while loop.

And It's not tail recursive. If it would be tail recursive this would have been a cracy mess. The game would crash when reaching the maximal stack size, since afaik Java / JVM hasn't got tail calls implemented yet.

Take a look at my development Blog: http://matheusdev.tumblr.com
Also look at my RPG Ruins of Revenge
Offline sproingie
« Reply #6 - Posted 2013-02-28 17:08:37 »

It's "tail recursive" in that run() reschedules itself then exits.  Similar to how javascript typically runs infinite loops.  It also doesn't have any way to take input or produce a return value, though I suppose it would be simple to modify it for either.  

Going back to my comment about sharing the executor, it's not a bad thing to experiment.  I'd encourage trying out that approach with several such runnables.  Next, drive their execution from a blocking queue, and you have an actor system.

Pages: [1]
  ignore  |  Print  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Try the Free Demo of Revenge of the Titans

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (67 views)
2013-05-17 21:29:12

alaslipknot (76 views)
2013-05-16 21:24:48

gouessej (108 views)
2013-05-16 00:53:38

gouessej (104 views)
2013-05-16 00:17:58

theagentd (115 views)
2013-05-15 15:01:13

theagentd (104 views)
2013-05-15 15:00:54

StreetDoggy (149 views)
2013-05-14 15:56:26

kutucuk (172 views)
2013-05-12 17:10:36

kutucuk (170 views)
2013-05-12 15:36:09

UnluckyDevil (179 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.079 seconds with 21 queries.