I have a question regarding the thread-safety. For example: I have RestController with two endpoints :
@RestController
class MyController {
@Autowired
MyService service;
@GetMapping
public void add(int i) {
service.add(i);
}
@GetMapping
public void delete(int i) {
service.delete(i);
}
}
@Component
class MyService {
private List<Integer> list;
public void add(int i) {
list.add(i);
}
public void delete(int i) {
list.remove(i);
}
}
As I understand, because MyService has scope singleton, his local variable list will share between threads and not thread-safety? If yes, what are the best practices to resolve this issue?