开发者社区> 问答> 正文

JFinal 线程安全求解:报错

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

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();
    }
  }




展开
收起
kun坤 2020-06-07 00:33:08 481 0
1 条回答
写回答
取消 提交回答
  • 问题解决了,但是不知道为什么,很奇怪。

    ContestController.rejudge() 调用judgeService.rejudgeContest() 调用judgeService.rejudge(),然后judgeService.rejudge()建立新线程。发现contest/rejudge被调用了两次。

    将ContestController.rejudge()改名为ContestController.rejudgeContest()问题解决。

    2020-06-07 00:33:13
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
多IO线程优化版 立即下载
JAVA反射原理以及一些常见的应用 立即下载
低代码开发师(初级)实战教程 立即下载