我需要从数据库读取一些数据,然后分别建立一个线程进行处理。
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(); } }
问题解决了,但是不知道为什么,很奇怪。
ContestController.rejudge() 调用judgeService.rejudgeContest() 调用judgeService.rejudge(),然后judgeService.rejudge()建立新线程。发现contest/rejudge被调用了两次。
将ContestController.rejudge()改名为ContestController.rejudgeContest()问题解决。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。