如何停止/限制某些方法的执行(〜当已经有某个对象正在调用该方法时)
我陷入了一种情况,我需要以多用户方式移动球,我正在使用 RMI 在移动球的分布式动画中。
我的目标是以某种方式移动多个球,以便多个客户端观察球的相同运动/位置,我使用的是远程对象的球对象。
我的问题是: 我正在调用 move 函数,该函数是远程的,并且客户端数量的增加会导致更频繁地调用该函数,并且由于调用次数的增加而导致球速越来越快这种方法来自不同的客户端。
有人可以推荐我,我该如何解决这个问题:当球已经在一个客户端上移动时,其他球不会调用该函数,而只是利用它。
这是代码:
public void start() {
Play = true;
Thread t = new Thread(this);
t.start();
}
public void run() {
while (Play == true) {
runball();
pit.repaint();
try {
Thread.sleep(50);
} catch (InterruptedException ie) {
stop();
}
}
}
public void runball() {
try {
aBall.setBounds(pit.getWidth(), pit.getHeight());
aBall.move();
}
catch (Exception e) {
e.printStackTrace();
}
}
这是我的远程移动方法:
public void move() throws RemoteException {
// ... Move the ball at the give velocity.
m_x += m_velocityX;
m_y += m_velocityY;
if (m_x < 0) { // If at or beyond left side
m_x = 0; // Place against edge and
m_velocityX = -m_velocityX;
} else if (m_x > m_rightBound) { // If at or beyond right side
m_x = m_rightBound; // Place against right edge.
m_velocityX = -m_velocityX;
}
if (m_y < 0) { // if we're at top
m_y = 1;
m_velocityY = -m_velocityY;
} else if (m_y > m_bottomBound) { // if we're at bottom
m_y = m_bottomBound;
m_velocityY = -m_velocityY;
}
}
有人可以指导我吗,这个设计中存在一些问题,我以错误的方式使用了 RMI?或者向我推荐一些可以实现我的目标的设计。
非常感谢,
吉比
i am stuck in a situation, where i need to move balls in multi-user way, i m using RMI
in a distributed animation of moving BALLS .
My goal is to move multiple balls in a way, so that multiple clients observe the same movement/position of balls, i m using ball object which is remote object.
My problem is: i am calling the move function, which is remote and increasing no of clients causes calling that function more frequently and it causes ball speed more and more because of increasing no of calls to this method from different clients.
can somebody please recommend me that how could i cope with this problem that when ball is already moving on one client then other ball don't call the function but just utilize that.
here is code :
public void start() {
Play = true;
Thread t = new Thread(this);
t.start();
}
public void run() {
while (Play == true) {
runball();
pit.repaint();
try {
Thread.sleep(50);
} catch (InterruptedException ie) {
stop();
}
}
}
public void runball() {
try {
aBall.setBounds(pit.getWidth(), pit.getHeight());
aBall.move();
}
catch (Exception e) {
e.printStackTrace();
}
}
and there is my remote move method:
public void move() throws RemoteException {
// ... Move the ball at the give velocity.
m_x += m_velocityX;
m_y += m_velocityY;
if (m_x < 0) { // If at or beyond left side
m_x = 0; // Place against edge and
m_velocityX = -m_velocityX;
} else if (m_x > m_rightBound) { // If at or beyond right side
m_x = m_rightBound; // Place against right edge.
m_velocityX = -m_velocityX;
}
if (m_y < 0) { // if we're at top
m_y = 1;
m_velocityY = -m_velocityY;
} else if (m_y > m_bottomBound) { // if we're at bottom
m_y = m_bottomBound;
m_velocityY = -m_velocityY;
}
}
Can somebody please guide me , there is some problem in this design and i m using the RMI in wrong way? or recommend me some design by which i can accomplish my goal.
thanks a lot,
jibby
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您的客户不应该告诉服务器移动球。
相反,应该有一个服务器线程以所需的速率移动球。客户端应该简单地询问服务器球当前在哪里。
I don't think your clients should be telling the server to move the ball(s).
Instead, there should be a server thread that moves the ball at the required rates. The clients should simply be asking the server where the ball currently is.