奇怪的链表/匿名类行为 - 添加时执行?
这个问题与如何排队并调用实际方法..。不管怎样,我决定(毕竟)采用匿名类的想法。问题是,当我将匿名类添加到链接列表时,它实际上立即调用execute()...但它不应该这样。稍后将调用 Execute()。无论如何,这就是我所拥有的:
private LinkedList<AgentAction> actions;
public boolean blockingSensor;
this.actions.add( new AgentAction(this) {
public void execute() {
//setRotationalVelocity(0);
kinematic.setWheelsVelocity(0,0);
this.agent.setBlockingSensors(false);
this.agent.printLCD("Turn, blocking = "+this.agent.blockingSensor);
}
public Object getValue() {
return null;
}
});
//this is essentially the main()
public void performBehavior()
{
//make sure to only call run() each tick, not every ms
if ( this.oldCounter < getCounter() )
{
if ( !isWorking() )
{
run();
}
this.oldCounter = getCounter();
this.actions.removeFirst().execute();
}
}
abstract class AgentAction
{
SimbadAgent agent;
public AgentAction(SimbadAgent a)
{
this.agent = a;
}
public abstract void execute();
public abstract Object getValue();
}
run() 是一个由子类实现的抽象方法。我只是不确定为什么它在添加时打印,而不是执行时打印。我理解这意味着performBehavior()实际上被执行了多次而不是每个tick执行一次,但事实并非如此。
This question is related to How to Queue and Call Actual Methods... Anyway, I've decided to (after all) go with the anonymous class idea. The problem is that when I ADD my anonymous class to the linked list, it's actually calling execute() immediately... and it shouldn't be. Execute() is to be called later. Anyway, this is what I have:
private LinkedList<AgentAction> actions;
public boolean blockingSensor;
this.actions.add( new AgentAction(this) {
public void execute() {
//setRotationalVelocity(0);
kinematic.setWheelsVelocity(0,0);
this.agent.setBlockingSensors(false);
this.agent.printLCD("Turn, blocking = "+this.agent.blockingSensor);
}
public Object getValue() {
return null;
}
});
//this is essentially the main()
public void performBehavior()
{
//make sure to only call run() each tick, not every ms
if ( this.oldCounter < getCounter() )
{
if ( !isWorking() )
{
run();
}
this.oldCounter = getCounter();
this.actions.removeFirst().execute();
}
}
abstract class AgentAction
{
SimbadAgent agent;
public AgentAction(SimbadAgent a)
{
this.agent = a;
}
public abstract void execute();
public abstract Object getValue();
}
run() is an abstract method that is implemented by a child class. I'm just not sure why it's printing when it's added, rather than executed. I understand this would imply that performBehavior() is actually being executed multiple times rather than once per tick, but that's not the case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
魔鬼在于细节。几乎可以肯定,您未显示的代码中的某处存在错误(我的猜测是
run
),但让我们解决更深层次的问题。这段代码看起来很多就像生产者-消费者问题。如果是这样,我建议查看 < code>java.util.concurrent:它充满了与并发相关的优点,这使得像这样的事情比尝试自己动手更容易。对于您的特定情况,它看起来像ScheduledExecutorService
可能是一个不错的选择。如果这不完全是你所需要的,我仍然建议你在包装中翻看一下;就像我说的,它充满了方便的东西,这些东西可能比你自己从并发原语构建的东西更容易使用。The devil is in the details. There's almost certainly a bug somewhere in the code you're not showing (my guess is
run
), but let's address a deeper point. This code looks a LOT like there producer-consumer problem. If so, I recommend checking outjava.util.concurrent
: it's overflowing with concurrency-related goodness that makes things like this WAY easier than trying to roll your own. For your particular case, it looks likeScheduledExecutorService
might be a good fit. If it's not exactly what you need, I still recommend poking around in the package; like I said, it's stuffed with handy things that will probably be a lot easier to work with than something you built yourself from the concurrency primitives.