Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
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 1247 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
« Reply #1 - Posted 2013-02-27 22:46:06 »

what is wrong with a simple while loop^^
this is just overkill
Offline sproingie

JGO Kernel


Medals: 202



« 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!
Legends of Yore - The Casual Retro Roguelike
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

JGO Kernel


Medals: 202



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

Offline matheus23

JGO Kernel


Medals: 106
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.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline sproingie

JGO Kernel


Medals: 202



« 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  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (26 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (13 views)
2014-07-29 10:59:34

E.R. Fleming (34 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36

Riven (44 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!