Reputation: 3717
This is a controller which @autowire TestRunner testRunner
. I think spring creates beans with singletone scope so testRunner will be shared among all threads.
This testRunner
usese setTestRunner
method to set some variables which are class level.
Will this approach create problem in multithreading ?
@Controller
@RequestMapping("/welcome")
public class HomeController {
@Autowire
TestRunner testRunner;
@RequestMapping(method = RequestMethod.GET)
public void addCustomerPage(User user) {
A a = new A();
a.set(user.get) ....
B b = new B();
b.set(user.get) ....
C c = new C();
c.set(user.get) ....
testRunner.setTestRunner(a, b, c);
Thread t = new Thread(testRunner);
t.start();
}
}
This is TestRunner class.
@Component
public class TestRunner implements Runnable{
private A a;
private B b;
private C c;
public void setTestRunner(A a, B b, C c){
this.a = a;
this.b = b;
this.c = c;
}
public void run() {
// use a,b,c.
}
}
Upvotes: 1
Views: 1559
Reputation: 2033
Yes it would,
Your test runner class is singleton, so it lives in the application scope thereby the same instance is shared among the whole application.
However you use it in a stateful manner, setting values to it depending on what you recieve in your request.
Either make your TestRunner class scope as prototype or don't autowire at all and create a new testrunner instance
@Scope(value = "prototype")
Upvotes: 3