Are methods (without using class variables) thread safe when using Spring Boot ? I came across carious links where they mentioned instance variables aren't safe always ?
My doubt is how can I create a race condition ? Is the below code thread safe ?And if yes then how can i make it thread - unsafe without using class variables
@RestController
public class GreetingController {
@Autowired
private GreetingService greetingService;
@GetMapping("/hello")
public void greeting(@RequestBody MyUser myUser) throws Exception {
greetingService.getData(myUser);
}
@Service
public class GreetingService {
@Autowired
private DBService dBService;
public void getData (MyUser m ) throws InterruptedException
{
dBService.getData(m);
}
@Repository
public class DBService {
public MyUser getData(MyUser myUser) throws InterruptedException {
System.out.println( "message before: " + myUser.getA() + " Thread : " +Thread.currentThread().getName());
Thread.sleep(18000);
System.out.println( "message after " + myUser.getA() + " Thread : " +Thread.currentThread().getName());
return myUser;
}