Re: java: concurrency question

  • From: Q <q@xxxxxxxxxxxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Tue, 23 Aug 2011 14:03:48 -0400

I'm not exactly a Java expert, but what you're wanting here sounds like an asynchronous networking library specific to creating client/server applications? In Python, we have Twisted. A bit of quick googling turned up Mina as a similar Java implementation potentially useful for you. Description: Apache MINA is a network application framework which helps users develop high performance and high scalability network applications easily. It provides an abstract ·event-driven · asynchronous API over various transports such as TCP/IP and UDP/IP via Java NIO.

http://mina.apache.org/
If you want something a little simpler, consider using or writing a simple implementation of an observer pattern event system. Then just send and receive signals between your bits so they're nice and decoupled.
     Q




On 8/23/2011 11:50 AM, Littlefield, Tyler wrote:
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.


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

Other related posts: