关闭

restful 的线程安全问题

978人阅读 评论(2) 收藏 举报
package com.ws.service.syn;


import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;


@Path("counter")
public class Counter {


public int counter = 0;
@GET
@Path("/count")
public String getCount(){
counter++;
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return String.valueOf(counter);
}
public int get(){
return counter;
}
public void set(int num){
this.counter = num;
}
@GET
@Path("/hello")
public String hello(){
return "xjiewenbo";
}
@GET
@Path("/{num}")
@Produces("text/plain")
public String getCounter(@PathParam("num")String num){
set(Integer.parseInt(num));

try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return String.valueOf(get());
}

}


public static void main(String[] args) throws IOException {
        URI ServerURI=UriBuilder.fromUri("http://localhost/").port(9999).build();
    startServer(ServerURI);
        System.out.println("服务已启动,请访问:"+ServerURI);
    }    

    protected static SelectorThread startServer(URI serverURI) throws IOException {
        final Map<String, String> initParams = new HashMap<String, String>();
        initParams.put("com.sun.jersey.config.property.packages","com.ws.service.syn");
        System.out.println("Grizzly 启动中...");
        SelectorThread threadSelector = GrizzlyWebContainerFactory.create(serverURI, initParams);     
        return threadSelector;
    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:476285次
    • 积分:6097
    • 等级:
    • 排名:第4320名
    • 原创:58篇
    • 转载:558篇
    • 译文:1篇
    • 评论:25条
    最新评论