0
开始:我是开源软件的新手。 (Apache-Tomcat/Java/Restletframework)线程安全和静态子类
这是我的问题: 我正在用Restlet框架构建一个应用程序。我不知道我的编码/方法 是否是线程安全的!?有人可以告诉我,我是否正确地编码?或者我绝望地失败了?
构建:
- CLASS A将被客户端调用。 (其余请求)
- CLASS A(路由器)将调用B类
- CLASS B是我的中央请求处理程序,此B类调用另一个C类
- CLASS C是被请求的实际服务,在这个例子中,登录服务 -
正如你所看到的登录子类是静态的。这是一个线程安全的结构吗?如果多个线程访问某些共享状态
问候
CLASS 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;
}
}
CLASS 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
}
}
因此,子类被定义为“静态”的事情不是线程不安全的perse? – user1887511
我也想知道第一个Restlet-Starter-Application中的“sychronized”关键字,我知道它是从新的Restlet-version中的子类createInboundRoot()中移除的......,我必须在较旧的Restlet版本,和/或不会影响线程安全性? – user1887511
我不知道restlet。阅读手册。另外,你没有任何“静态子类”。你有一个静态方法。一个只使用局部变量的静态或不静态方法本质上是线程安全的。如果这是登录方法的情况,并且如果queryparams对象不在线程之间共享(并且看起来它不在线程之间共享),那么此方法是线程安全的。 –