首先:我是开源软件的新手。(Apache/ Java / Restletframework)
下面是我的问题:我正在用Restlet框架构建一个应用程序。我不知道我的编码/方法是否线程安全!?有人能告诉我我的编码方式是否正确吗?还是我毫无希望地失败了?
建造:
如您所见,登录子类是静态的。这是一个线程安全的结构吗?
问候
A类
public class MyStartApplication extends Application {
//Creates a root Restlet that will receive all incoming calls.
@Override
//public synchronized Restlet createInboundRoot() { //synchronized?
public Restlet createInboundRoot() {
//Create a router that routes each call to a new instance of a Resource.
Router router = new Router(getContext());
// First we use MODE_START_WITH to determine the requested destination
// A TRAPDOOR for all requests for this TEST
// We reroute it to THE CENTRAL RESTLET-WRAPPER
TemplateRoute route = router.attach("/testmywrapper/", RestletWrapper.class);
route.getTemplate().setMatchingMode(Template.MODE_STARTS_WITH);
// Return the response to caller
return router;
}
}B类
public class RestletWrapper extends ServerResource {
@Get
public JSONObject start() {
JSONObject returnObj = null;
switch(operation){
case "login":
returnObj= LoginUser.login(queryparams);
break;
}
Return returnObj
}
}C类
public class LoginUser {
public static JSONObject login(JSONObject queryparams) throws Exception {
do some stuff
return object
}
}发布于 2012-12-08 10:57:43
如果多个线程访问某些共享状态,则线程安全性可能会出现问题。
除非隐藏在“做一些事情”后面的代码使用静态字段或单线程,否则不会出现线程安全问题:所有变量都是登录方法的本地变量,因此在线程之间不共享。
https://stackoverflow.com/questions/13776622
复制相似问题