views:

50

answers:

2

There are a list of tasks that are time sensitive (but "time" in this case is arbitrary to what another program tells me - it's more like "ticks" rather than time). However, I do NOT want said methods to evaluate immediately. I want one to execute after the other finished. I'm using a linked list for my queue, but I'm not really sure how/if I can access the actual methods in a class without evaluating them immediate.

The code would look something like...

LinkedList<Method> l = new LinkedList<Method>();
l.add( this.move(4) );
l.add( this.read() );
l.removeFirst().call();
//wait 80 ticks
l.removeFirst().call();

move(4) would execute immediately, then 80 ticks later, I would remove it from the list and call this.read() which would then be executed.

I'm assuming this has to do with the reflection classes, and I've poked around a bit, but I can't seem to get anything to work, or do what I want. If only I could use pointers...

To clarify: These commands will be called by another class that does not know anything about the timing, which is why this class has to handle it. It will simply call methods that are expected to be executed in sequence. Some of the methods do not return anything but change state.

+2  A: 

PriorityBlockingQueue is your friend. Normally to get something approximating first class functions in java you'd use a small interface such as runnable and extend it in an (anonymous inner) class.

CurtainDog
Is there really no better way to do so? That's just so ugly...
alleywayjack
It's certainly a little clumsy to approximate, but it doesn't have to be horribly cumbersome. You could have an interface with one method. Unfortunately, you'd have to make a class for each "task" that would implement that interface.
Jonathon
It's not so bad really, the only thing you are repeating unnecessarily is the method signature. Everything else is a product of static typing, which I vastly prefer over other forms.
CurtainDog
A: 

You can certainly do something just like your example. You could do something using interfaces and classes and forgo reflection, too. I don't know what would work best for your situation. Here's how you can do it with reflection.

Check out the Class class and the Method class.

// Get the Class of the type you're grabbing methods from
Object target = new SomeObject();
Class theClass = target.getClass();
// Or, use the class literal:
Class theClass = SomeObject.class;

// Get the methods you want
// You need to know the method name and its parameter types.
Method aMethod = theClass.getMethod("move", Integer.class);

List<Method> myMethods = new LinkedList<Method>();
myMethods.add(aMethod);

// Later, you need an object on which to invoke a method.
Method currentMethod = myMethods.get(0);
currentMethod.invoke(target, 4);
Jonathon
That's great! Thanks! Just out of curiosity, do you happen to know which is faster at run-time - the inner classes, or the reflection libraries?
alleywayjack
I don't know, but my guess is reflection would be slower. Whip up a test case to be sure. Go with whichever solution is cleaner unless performance is critical. I haven't tested what I wrote in my answer, it's from memory, but it's close to being correct.
Jonathon
Will do, thanks again for your help.
alleywayjack
To be honest I used to love reflection too, and then i largely got over it. Its great for injecting depencies but in this case the tradeoff isn't worth it, IMHO. Just wait till you start handling all the exceptions (three from memory) and you'll see what I mean.
CurtainDog
@CurtainDog I agree, but there are times when reflection is a really handy tool to use. I think its best hidden away in a dark, stable corner of your code.
Jonathon