基本上我有一個簡單的新聞應用程序,現在我想有新聞列表自動更新所有用戶每當有人添加或刪除新聞時,它有點工作,但有時我得到ConcurrentModificationException,我只需要編寫此方法的幫助:春季長輪詢ConcurrentModificationException
@GetMapping("/pollnews")
@ResponseBody
public DeferredResult<ModelAndView> poll(Model model){
DeferredResult<ModelAndView> result = new DeferredResult<>();
new Thread(new Runnable() {
@Override
public void run() {
while(true){
if(changeOccured){
changeOccured = false;
model.addAttribute("news", newsService.getAllNews());
result.setResult(new ModelAndView("partial"));
break;
}
}
}
}).start();
return result;
}
堆棧跟蹤:
Exception in thread "Thread-13" java.util.ConcurrentModificationException
at java.util.ArrayList.sort(ArrayList.java:1456)
at com.newsapp.SpringNews.Service.NewsService.getAllNews(NewsService.java:25)
at com.newsapp.SpringNews.Controller.ViewController$1.run(ViewController.java:125)
at java.lang.Thread.run(Thread.java:748)
你指定在哪一行發生異常? – Lino
它發生在newsService類和getAllnews()方法中,但沒關係,因爲我認爲我寫這個poll()方法錯了,問題可能就在那裏...... –
你可以添加堆棧跟蹤到你的問題嗎?因爲我的方法沒有看到任何錯誤 – Lino