如何在java中排队并调用实际方法(而不是立即评估)?

发布于 2024-09-05 14:18:28 字数 601 浏览 3 评论 0原文

有一个对时间敏感的任务列表(但在这种情况下,“时间”对于另一个程序告诉我的内容是任意的 - 它更像是“滴答声”而不是时间)。但是,我不希望立即评估所述方法。我希望一个在另一个完成后执行。我在队列中使用链表,但我不确定如何/是否可以访问类中的实际方法而不立即评估它们。

代码看起来像...

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) 将立即执行,然后 80 个时钟周期后,我会将其从列表中删除并调用 this.read()然后将被执行。

我假设这与反射类有关,并且我已经研究了一下,但我似乎无法让任何东西起作用,或者做我想做的事。如果我可以使用指针...

澄清一下:这些命令将由另一个不知道任何时间的类调用,这就是为什么这个类必须处理它。它只会调用预期按顺序执行的方法。有些方法除了更改状态外不返回任何内容。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

止于盛夏 2024-09-12 14:18:28

PriorityBlockingQueue 是你的朋友。通常,要获得类似于 Java 中的一流函数的东西,您需要使用一个小型接口(例如 runnable),并在(匿名内部)类中扩展它。

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.

雨落□心尘 2024-09-12 14:18:28

你当然可以做一些像你的例子一样的事情。您也可以使用接口和类来做一些事情,并且放弃反射。我不知道什么最适合您的情况。以下是如何通过反思来做到这一点。

查看 Class 类和 < a href="http://java.sun.com/javase/6/docs/api/java/lang/reflect/Method.html" rel="nofollow noreferrer">Method 类。

// 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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文