spring - Thread safety with @Scheduled and @Async, Java -
i took code: private queue<object> myqueue = new concurrentlinkedqueue(); public enqueue(object obj) { myqueue.add(obj); } @scheduled(fixedrate=1000) public void publish() { final list objstopublish = lists.newarraylist(); final int listsize = myqueue.size(); (int = 0; < listsize; i++) { objstopublish.add(myqueue.poll()); } expensivewriteoperation(objstopublish); } however, it's problem if publish() takes away control during other operations running in software, tried make expensive call asynchronous, so: private queue<object> myqueue = new concurrentlinkedqueue(); public enqueue(object obj) { myqueue.add(obj); } @scheduled(fixedrate=1000) public void publish() { final list objstopublish = lists.newarraylist(); final int listsize = myqueue.size(); (int = 0; < listsize; i++) { objstopublish.add(myqueue.poll()); } work(objstopublish); } @async void work(list objstopublish) { expensivewriteoperation(objstopublish);...