1
回答
JFinal 线程安全求解
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

我需要从数据库读取一些数据,然后分别建立一个线程进行处理。

Controller调用Service的方法获取Model List,然后发现有两个线程。

两个线程都在读取Model List的内容,每个元素都被重复地添加到Thread List里面。

我的Controller继承了一个公共的Controller。

请问怎么解决?

199 [2014-04-20 20:32:45]-[http-bio-8080-exec-3]-[com.power.oj.judge.JudgeService.rejudgeContest(JudgeService.java:160)]
200 [INFO]: sid: 86178 tid: 43
201 [2014-04-20 20:32:45]-[http-bio-8080-exec-4]-[com.power.oj.judge.JudgeService.rejudge(JudgeService.java:86)]
202 [INFO]: Add: 86176  14
203 [2014-04-20 20:32:45]-[http-bio-8080-exec-4]-[com.power.oj.judge.JudgeService.rejudgeContest(JudgeService.java:160)]
204 [INFO]: sid: 86177 tid: 45
205 [2014-04-20 20:32:45]-[Thread-9]-[com.power.oj.judge.UestcJudgeAdapter.RunProcess(UestcJudgeAdapter.java:76)]
206 [INFO]: Total run time: 4 ms, max run time: 4 ms
207 [2014-04-20 20:32:45]-[http-bio-8080-exec-3]-[com.power.oj.judge.JudgeService.rejudge(JudgeService.java:86)]
208 [INFO]: Add: 86178  15
209 [2014-04-20 20:32:45]-[http-bio-8080-exec-3]-[com.power.oj.judge.JudgeService.rejudgeContest(JudgeService.java:160)]
210 [INFO]: sid: 86179 tid: 43
211 [2014-04-20 20:32:45]-[http-bio-8080-exec-4]-[com.power.oj.judge.JudgeService.rejudge(JudgeService.java:86)]
212 [INFO]: Add: 86177  16
213 [2014-04-20 20:32:45]-[http-bio-8080-exec-4]-[com.power.oj.judge.JudgeService.rejudgeContest(JudgeService.java:160)]
214 [INFO]: sid: 86178 tid: 45
215 [2014-04-20 20:32:45]-[http-bio-8080-exec-3]-[com.power.oj.judge.JudgeService.rejudge(JudgeService.java:86)]
216 [INFO]: Add: 86179  17
217 [2014-04-20 20:32:45]-[http-bio-8080-exec-4]-[com.power.oj.judge.JudgeService.rejudge(JudgeService.java:86)]
218 [INFO]: Add: 86178  18



public void rejudgeContest(Integer cid)
  {
    contestService.reset(cid);
    List<ContestSolutionModel> solutionList = Collections.synchronizedList(solutionService.getSolutionListForContest(cid));
    synchronized(solutionList)
    {
      Iterator<ContestSolutionModel> it = solutionList.iterator();
      while (it.hasNext())
      {
        rejudge(it.next());
      }
    }
  }



public void rejudge(Solution solution)
  {
    JudgeAdapter.addSolution(solution);
    if (JudgeAdapter.size() <= 1)
    {
      JudgeAdapter judge = null;
      judge = new UestcJudgeAdapter();
      new Thread(judge).start();
    }
  }




举报
魔神翼
发帖于4年前 1回/467阅
顶部