java:竞争条件 - 有没有办法确保多行代码将一起执行? [英] java: race conditions - is there a way to make sure several lines of code will be executed together?

查看:67
本文介绍了java:竞争条件 - 有没有办法确保多行代码将一起执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个注册页面,它接收令牌并解析它们并在参数适用时登录用户.

I have a registration page that receives tokens ad parse them and login the user if the parameters apply.

在我检查令牌的时间到我从数据库中删除令牌的时间之间,另一个用户可以使用相同的令牌登录.有没有办法确保特定范围的代码行不受干扰地执行,这样我就不会有竞争条件问题?

Between the time that i checked the token, to the time that i removed the token from the db, another user can use the same token to login. is there a way to make sure that specific range of lines of code will be executed with not interference, so i won't have race condition problem ?

谢谢

我有两台服务器.

  1. apache tomcat 6
  2. red5 v0.9(基于 Java 的免费 Flash 媒体流和通信服务器)

我正在为 Facebook 编写游戏应用程序.

I'm writing a game application for Facebook.

游戏本身是用 adobe flash builder 2 编写的.

the game itself is written in adobe flash builder 2.

游戏本身将使用 red5 服务器提供服务.问题是 red5 没有收到请求和响应标头,因此无法检索会话信息以用于从 Facebook 获取信息.

the game itself will be served using the red5 server. the problem is that red5 does not receive the request and response headers and because of that cannot retrieve session information to use in order to fetch information from facebook.

为了解决用户连接到tomcat服务器的问题,该页面检查facebook相关信息的会话并使用tinyFBClient连接到facebook,并将信息存储在mysql db(用户详细信息)中,以便使确保这个用户就是要连接到 red5 的用户.

to resolve the issue the user connect to the tomcat server, that page checks the session for facebook related information and uses tinyFBClient to connect to facebook, and to store the information in the mysql db (user details), in order to make sure that this same user is the user that's going to connect to red5.

一旦创建了令牌.tomcat 页面显示一个对象 HTML 元素以显示相关的 SWF 文件(游戏文件).tomcat 页面将令牌传递给该 SWF 文件.加载 SWF 文件后,它会获取该令牌并使用它连接到 red5 并查找用户信息.

once a token is created. the tomcat page displays an object HTML element in order to show the relevant SWF file (game file). the tomcat page passes the token to that SWF file. once the SWF file is loaded, it takes that token and uses it to connect to red5 and for it to find out the user information.

我希望这些描述能帮助您了解我的需求.感谢大家的支持!

I hope that description helps you understand my needs. thanks for all of your support!

推荐答案

您可以使用 锁定 对象.

使用 Lock 对象比更原始的同步块和方法有优势.

Using Lock objects has advantages over the more primitive synchronized blocks and methods.

这篇关于java:竞争条件 - 有没有办法确保多行代码将一起执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆