java: concurrency question

  • From: "Littlefield, Tyler" <tyler@xxxxxxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Tue, 23 Aug 2011 09:50:43 -0600

Hello all:
Sorry for all the random Java questions. I've kind of dove into it, and am trying to get a grip with everything. So, here's my question. First I have set things up so that it uses the Executors to run two different threads; the server thread, and the engine thread which is responsible for updating things in-game. Now, I'm curious of two things. First, as always, I'd like to know if there's a better way to write this. Second, I'm curious if I can just check to see if any of the threads have exited, as opposed to what I do in my while loop, and whether there's a way to notify the controling thread of events. For example, my Engine will have a shutdown command that can be called. This works well enough in a single-threaded app--the engine's shutdown method is called, the engine tells the server "hey, tell everyone we're going to dye," waits for the server, and kills everything else. But in a multi-threaded application, Engine shouldn't know about server, at least I don't think it should. So when shutdown is called, or something happens i need some way to notify the controling thread so it can try to stop the rest of the threads and exit cleanly.
Anyway, here's my Main.java, which is responsible for starting everything.
    public static void main(String [] args) {
        try {
FileHandler fhandle = new FileHandler("jmud.log.%g", 1000000, 10);
            Logger log = Logger.getLogger("jmud");
            log.addHandler(fhandle);
        } catch (IOException ex) {
            System.out.println("Caught IOException: "+ex.getMessage());
            return;
        }

        Server server = new Server();
        server.listen(4000);
        Engine engine = new Engine();

        ExecutorService taskExecutor = Executors.newCachedThreadPool();
CompletionService<Integer> completion = new ExecutorCompletionService<Integer>(taskExecutor);
        Future<Integer> servertask = completion.submit(server);
        Future<Integer> enginetask = completion.submit(engine);

        while (!servertask.isDone() || enginetask.isDone()) {
        }
    }

Ideas, pointers, tips... Anything is welcome here. Thanks in advance.

--

Take care,
Ty
my website:
http://tds-solutions.net
my blog:
http://tds-solutions.net/blog
skype: st8amnd127
My programs don't have bugs; they're randomly added features!

__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind

Other related posts: