Multithreading problem

Hello! I am using the enqueue() method for the first time (since i’m working on a server), and I have a problem.

Inside the Callable object, I must call a method and pass some arguments to it. Here:

public void messageReceived(Client client, Message m) {
    if(m instanceof PlayerPositionMessage) {
        PlayerPositionMessage pos = (PlayerPositionMessage) m;
        app.enqueue(new Callable<Object>() {
            public Object call() throws Exception {
                enemys.playerPosition(pos.getLocation(), pos.getDirection, pos.getNome);
                return null;
            }
        });
    }

My problem is that the “pos” object is not accessible inside the call() method. I could create a local variable, but it doesn’t seem appropriate since this is a message that is constantly being updated.

So, how do I do it?

Thanks,
Ev1lbl0w

make it final:

final PlayerPositionMessage pos = (PlayerPositionMessage) m;

Thanks!

Another option is to stop using anonymous inner classes and make real Callables that take the parameters needed. Which is easier will depend on style and other factors… personally I almost never use anonymous inner classes.

Ok, I think it would be better to create my own classes from the Callable too. Thanks!

If you’re using Java 8 and you really wanted to shine it on you could use Lambda expressions :smile:

http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/Lambda-QuickStart/index.html

Java 8 is really cool, but I prefer to code in Java 7, because I installed Java 8 and most of my games stopped working (Minecraft, for example). I will upgrade to Java 8 when the most part of the people code with the Java 8. But yeah, didn’t knew Lambda expressions could be used to multithreading (actually I don’t know anything about lambda). Thanks!