Java 中的线程状态机

发布于 2024-08-17 02:16:12 字数 938 浏览 4 评论 0原文

有没有办法将线程保持在等待更改的状态? 我的意思是等待发生一些事情(更改变量,调用方法等..) 也许它需要使用事件侦听器或同步对象/方法。

像这样的状态机的常用方法

statemachine example

它使用 do{..}while(true) 循环,可以适用于单线程(并且没有 GUI)应用程序,但它不能与线程一起使用。 (至少你想为每个线程状态机使用一个核心)

因此,为了避免处理器消耗任务,一种简单(且丑陋)的方法 是一个“定期检查器”,我的意思是添加一个睡眠 另一个想法是定义一个同步对象并使用等待而不是线程睡眠

示例:

do{
    Switch(state)
    {         
        case STATE_A:
            //..A things                         
            break;

        case STATE_B:
            //..B things                         
            break;

        ...

        case STATE_Z:
            //..Z things                         
            break;
    }
    // Here!  =>   wait()?  Thread.sleep(TIME_CONST)? //Hold and yield
}while(powerOn);

缺点是使用同步增加了复杂性,或者 TIME_CONST 内发生的任何事情都是不可见的

我想知道这样做的其他想法, 谢谢 !

Is there a way of Holding a thread in a State waiting for changes?
I mean wait tll something happend (change var, call method, etc..)
perhaps it needs using Event Listeners, or synchronized objects/methods..

The usual aproach for statemachines like this

statemachine example

That uses a do{..}while(true) loop that could work for singled threaded(and no GUI) application but it CANT'T be used with threads..
(at least you want to use a core for every threaded state machine)

So to avoid that processor consuming task a simple (and ugly) way
is a "periodic checker", I mean adding a Sleep
other idea is defining a synchronized object and using wait instead of thread Sleep

example:

do{
    Switch(state)
    {         
        case STATE_A:
            //..A things                         
            break;

        case STATE_B:
            //..B things                         
            break;

        ...

        case STATE_Z:
            //..Z things                         
            break;
    }
    // Here!  =>   wait()?  Thread.sleep(TIME_CONST)? //Hold and yield
}while(powerOn);

The drawback is adding complexity using synchronized or that anything that happen within TIME_CONST would be invisible

I would like to know other ideas for doing this,
thanks !

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

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

发布评论

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

评论(4

太阳哥哥 2024-08-24 02:16:12

是的,您将使用wait/notify。这就是它的用途。

或者也许你的状态机不需要有自己的线程。当发生有趣的事情时,其他线程可以调用状态机上的更新方法。

Yes, you would be using wait/notify. That is what it is for.

Or maybe your state machine does not need to have its own thread. Other threads could call an update method on the state machine when something interesting happens.

夏花。依旧 2024-08-24 02:16:12

如果您在 Umple 中指定一个排队状态机,那么将为您生成相应的代码。将 umple 状态机指定为排队会专门创建一个等待事件(命名方法调用)的线程。类中的其他代码可以在原始线程中同时发生。关键字“queued”应该放在状态机定义之前。

有关在 Umple 中创建状态机的更多信息,请参阅 http://statemachines.umple.org。在这种情况下,您可以将 Umple 视为 Java(以及其他语言)的预处理器。

If you specify a queued state machine in Umple then the code for this will be generated for you. Specifying an umple state machine as queued specifically creates a thread that waits for events (named method calls). Other code in the class can happen concurrently in the original thread(s). The keyword 'queued' should be placed just before the definition of the state machine.

See http://statemachines.umple.org for more information about creating state machines in Umple. You can consider Umple as just a preprocessor for Java (and for other languages) in this context.

波浪屿的海角声 2024-08-24 02:16:12

您还可以使用 Lock 和 条件(Java 1.5+)。它就像 wait/notify 并且 javadoc 中有一个示例

You can also use Lock and Condition (Java 1.5+). It's like wait/notify and there is an example in the javadoc

勿挽旧人 2024-08-24 02:16:12

尝试在事件源线程和FSM实施例线程。这对于 2 线程实现来说已经足够了。

Try using a Pipe between the event source thread and the FSM embodiment thread. This is sufficient for a 2 threaded implementation.

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