关于在JSP中的一个方法 同时只允许一个客户访问应该怎么办?
public synchronized String getComplieResult(SubmitBean submitBean,UserBean user,FormatFile formatFile,String language)
{
   p=Runtime.getRuntime().exec("gcc "+"\""+file.toString);
   p.waitFor();
   ..... 
   .....
}
这个方法 已经加了 synchronized 
可是为什么老是可以在任务管理器中中看到好几个 gcc.exe在运行???
难道不是一次只有一个人访问的吗??还有就是那个GCC  也真晕,GCC编译怎么还要这么长时间??系统没调度??
怎么会出现这个GCC  还不会结束???
编译应该是很快的撒

解决方案 »

  1.   

    hehe, 这个错误比较常见。你给 getComplieResult() 加上 synchronized,那么,对“同一个”对象的 getComplieResult() 调用会被同步。但在 Web 环境下,不同的 HTTP request 访问的可能是不同对象的 getComplieResult(),所以就失效了。
      

  2.   

    楼上正解~~你要是想在分布式环境下保证同一时刻下只有一个客户访问,可以用设计模式的单例(Singleton)模式,即让这个方法所在的类成为单例类,也就是说只能生成一个实例,然后此方法再用synchronized关键词保证同一时刻只能同一个客户在调用。Good luck!
      

  3.   

    public class Test{
       private static Test test=new Test();
       private  Test(){}
       public  static Test getTest(){return test};public synchronized String getComplieResult(SubmitBean submitBean,UserBean user,FormatFile formatFile,String language)
    {
       p=Runtime.getRuntime().exec("gcc "+"\""+file.toString);
       p.waitFor();
       ..... 
       .....
    }}//外面调用
    Test.getTest().getComplieResult(...);
      

  4.   

    你们三位大虾说的 都是单例模式吧。
    呵呵,我昨天查资料的时候,也这么说。
    或许只能这样吧....谢谢你们。
    有谁了解GCC吗?  很郁闷