简体   繁体   English

我们可以为Java接口实施超时吗?

[英]Can we enforce timeout for java interface implementation?

I'm working on a game server based in java. 我正在基于Java的游戏服务器上工作。 My design on the server side is as follows. 我在服务器端的设计如下。
The RuleEnforcer is passed in a list of IPlayers, and it initiates communication with the players. RuleEnforcer在IPlayers列表中传递,并启动与播放器的通信。 IPlayers is an interface, and i imagine implementing a dummy Player for testing purpose, a "smart" player, and also a player that eventually interacts with a remote client UI interface. IPlayers是一个接口,我想实现一个用于测试目的的虚拟播放器,一个“智能”播放器以及一个最终与远程客户端UI界面进行交互的播放器。 In RuleEnforcer when player methods are called, it needs to ensure that the method returns (on time). 在RuleEnforcer中,调用播放器方法时,需要确保该方法(按时)返回。 eg: 例如:

for(player : players) {
    player.giveAcard(card);
}

giveACard has no return type, and the each player needs to just save the card and return immediately. GiveACard没有返回类型,每个玩家只需要保存卡并立即返回即可。 But if a player implementation sleeps on the method call, the game is stuck. 但是,如果玩家实现在方法调用上处于休眠状态,则游戏将停滞不前。 How do I account for that in my code ? 如何在我的代码中说明呢? One approach I can think of is to call player interactions in a different thread, and use wait. 我能想到的一种方法是在另一个线程中调用玩家交互,然后使用等待。 Is there any design that is applicable to this scenario, or is my approach wrong ? 是否有适用于此方案的设计,还是我的方法错误?

yes you can create by this way. 是的,您可以通过这种方式创建。 create a constructor 创建一个构造函数

SimpleTimeLimiter()

Constructs a TimeLimiter instance using a Executors.newCachedThreadPool() to execute proxied method calls. 使用Executors.newCachedThreadPool()构造一个TimeLimiter实例以执行代理方法调用。 method detail with new proxy.... 使用新代理的方法详细信息。

public <T> T newProxy(T target,
             Class<T> interfaceType,
             long timeoutDuration,
             TimeUnit timeoutUnit)

then call with timeout... 然后超时通话...

callWithTimeout
public <T> T callWithTimeout(Callable<T> callable,
                    long timeoutDuration,
                    TimeUnit timeoutUnit,
                    boolean amInterruptible)

for reference use...new proxy.. 供参考...新代理

public <T> T newProxy(T target,
             Class<T> interfaceType,
             long timeoutDuration,
             TimeUnit timeoutUnit)

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

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