Created a Java application on spring boot. Declared a variable int x=0;
Opened it through the browser, and wrote x=1; to the variable x x=1;
I opened the same application in the second tab of the browser, and already in it through the web interface, for example through an html element, assigned x=2 .
If the third client in the third tab writes in x=3 , then the previous two clients will also have x equal to three, and for example, when you click on a button to view the result, the code will run
if (x = 0) System.out.println("x = 0") elseif (x = 1) System.out.println("x = 1") else if (x = 2) System.out.println("x = 2") else if (x = 3) System.out.println("x = 3") and all three will display the same thing, namely: x = 3;
Those. one application is able to run on only one thread. How to solve this problem?
xdeclared inside the method, then the memory for it is allocated in the stack frame and the variable exists for exactly as long as the method runs. If a variable is declared as a class field, memory for it is allocated on the heap and it will not be removed until the server is stopped. - Sergey Gornostaev