简体   繁体   English

暂停/恢复线程中的任意计算

[英]Pause/Resume arbitrary computation in thread

I'm making a programming game where the player can program their allies' behavior. 我正在制作一款编程游戏,玩家可以在其中编程盟友的行为。 The player writes the body of the decide() function for a given ally, which can be filled out with any java code but has to return an action. 玩家为给定的盟友编写了define()函数的主体,可以用任何Java代码填充该主体,但必须返回一个动作。 I would like to give each ally a set, restricted amount of computation per tick so 1) adding more entities doesn't slow down the game too much, and 2) The time an entity spends computing is reflected in game, so if an ally spends more time "thinking" it will act less often. 我想给每个盟友一个固定的,每刻有限的计算量,因此1)增加更多实体不会减慢游戏的速度,以及2)实体花费在计算上的时间会反映在游戏中,因此如果盟友花更多的时间在“思考”上,它将减少执行频率。 My inspiration for how this should work is Battlecode , which gives units a set amount of bytecode per turn, then just pauses the computation and makes the programmer deal with noticing when things have changed. 对它的工作方式的灵感是Battlecode ,它每转为单位提供一定量的字节码,然后只是暂停计算并让程序员在事情发生变化时处理通知。

My question is how I can pause and resume an entity which is executing the decision function in a thread. 我的问题是我如何暂停和恢复在线程中执行决策功能的实体。 I understand the 'proper' way to do this is to set a flag telling the thread to pause and have it check occasionally, but since I can't force the player to check for a flag within the decide() function, I'm not sure how to pause the thread. 我知道执行此操作的“正确”方法是设置一个标志,告诉线程暂停并偶尔对其进行检查,但是由于我无法强迫玩家在define()函数中检查标志,因此不知道如何暂停线程。 The entities are only looking at a fixed representation of the world and just have to return an enum value, so I don't think they should have locks on anything, but I'm hoping there's a better way to do this than using the deprecated thread pausing methods. 实体只是在看世界的固定表示,而只需要返回一个枚举值,所以我认为它们不应该锁定任何东西,但是我希望有一个比使用不赞成使用的方法更好的方法线程暂停方法。 I'm open to changing how the player has to write code, but I can't think of a way to do it while still hiding the pause flag checks from the user without making writing the decision loop confusing and onerous. 我乐于改变玩家必须编写代码的方式,但是我想不出一种方法来实现,同时仍然向用户隐藏暂停标志检查,而不会导致编写决策循环令人困惑和繁重。 There must be some way to do this, since Battlecode does it, but I'm at a loss searching online for details as to how. 必须使用某种方法来执行此操作,因为Battlecode会这样做,但是我不知所措地在网上搜索有关操作方法的详细信息。

If you want to 'pause' the current thread, java.util.concurrent.locks.LockSupport may helps you. 如果要“暂停”当前线程,则java.util.concurrent.locks.LockSupport可能会对您有所帮助。 If you want to pause other threads, I thinks it's not in the scope of java design, you can only interrupt another thread, or set a flag, not pause. 如果要暂停其他线程,我认为这不在Java设计范围内,则只能中断另一个线程或设置一个标志,而不能暂停。

You can't "pause a thread", and you can't expect players to abide by your request to "play nice" and check for flags etc. You can interrupt another thread, but the code running in the thread can easily recover from this. 您不能“暂停线程”,也不能期望玩家遵守您的要求“打得好”并检查标志等。您可以中断另一个线程,但是在该线程中运行的代码可以轻松地从中恢复这个。 So, you need a way for the controlling thread to retain control. 因此,您需要一种让控制线程保留控制权的方法。

Instead of worrying about what threads are doing, you could: 不必担心线程在做什么,您可以:

  • give the player threads a maximum time to return the action, and if they don't return in time, execute a "default action" 给玩家线程最大的时间来返回动作,如果他们没有及时返回,则执行“默认动作”
  • keep a record of how much time they spent calculating and call them more often is they use less time etc 记录他们花了多少时间进行计算,并且更频繁地打电话给他们,因为他们使用的时间更少等

Most of these types of concerns are catered for by java.util.concurrent library. 这些关注点中的大多数都由java.util.concurrent库解决。 Check out: 查看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM