ITeye博客

线程安全

SandOfSitsang · 2013-03-10 20:22 · Issue List · 编程语言 · Markdown

private Set<Long> currentlyInUsing = new HashSet<long>();

public void getPdf(){
   if (currentlyInUsing .contains(processId)) {
     return null;
   }

   currentlyInUsing .add(processId);

   // pdf generating .... do something consume lots of time

   currentlyInUsing .remove(processId);
}

 第一个用户进入后,

马上第二个用户也来了, 第二个用户会进入 return null 代码吗?

 

--测试结果是第二个用户使第一用户的pdf出错,第二个用户生成成功。并且第二个用户没有进入 return null代码。

 

正解应该还是对方法用synchronised.

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。