--A concept in multithreaded programming. --A program that can be run in multiple threads without any problems. --No problem occurs when multiple threads execute code in parallel.
When multiple threads access a shared data, only one thread must access the shared data at a time to ensure security.
If you do not make it thread-safe for applications that are supposed to be used by multiple people at the same time, you will be in a state where "Mr. A unintentionally acquires data that only Mr. A should be able to see". I will end up.
--For Java, use local variables instead of class variables and instance variables. --However, there is no problem with constants other than local variables that are used statically and do not change.
Local variables are stored in a memory area called the Java stack area, so they can only be accessed by one thread. In other words, it is held in a different memory area depending on the thread, and it cannot be referenced or rewritten by other threads. By the way, class variables and instance variables are held in a memory area called the heap area, and that area is referenced by multiple threads.
Be aware that even if you don't use class variables and instance variables in your Java program, you will unintentionally use them in JSP.
<%! int count = 0; %>
The JSP is compiled into a Servlet at run time, but the above declaration is expanded as an instance variable.
Recommended Posts