Java 中的线程状态机
有没有办法将线程保持在等待更改的状态? 我的意思是等待发生一些事情(更改变量,调用方法等..) 也许它需要使用事件侦听器或同步对象/方法。
像这样的状态机的常用方法
它使用 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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,您将使用
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.如果您在 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.
您还可以使用 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尝试在事件源线程和FSM实施例线程。这对于 2 线程实现来说已经足够了。
Try using a Pipe between the event source thread and the FSM embodiment thread. This is sufficient for a 2 threaded implementation.