奇怪的链表/匿名类行为 - 添加时执行?

发布于 2024-09-07 02:53:40 字数 1286 浏览 8 评论 0原文

这个问题与如何排队并调用实际方法..。不管怎样,我决定(毕竟)采用匿名类的想法。问题是,当我将匿名类添加到链接列表时,它实际上立即调用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 技术交流群。

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

发布评论

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

评论(1

多情出卖 2024-09-14 02:53:40

魔鬼在于细节。几乎可以肯定,您未显示的代码中的某处存在错误(我的猜测是 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 out java.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 like ScheduledExecutorService 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.

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