What is thread safe (with Java)

the meaning of the word

--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.

Then what to do

--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.

Specifically, what is happening inside

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.

Caution

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

What is thread safe (with Java)
What is Java <>?
What is Java
What is Java Encapsulation?
Thread safe summary ~ Java ~
What is Java technology?
What is Java API-java
[Java] What is flatMap?
[Java] What is JavaBeans?
[Java] What is ArrayList?
What is Java Assertion? Summary.
DateFormat is not thread safe
What is a Java collection?
Java thread safe for you
[Java] What is jaee j2ee?
[Java] What is class inheritance?
[Java basics] What is Class?
What is java escape analysis?
What is JVM (Java Virtual Machine)?
thread safe process in java language
[Java] What is Concurrent Modification Exception?
What I learned with Java Gold
What is a lambda expression (Java)
What I learned with Java Silver
Java local variables are thread safe
What is a class in Java language (3 /?)
Learn Java with "So What" [For beginners]
What is the best file reading (Java)
What is a class in Java language (1 /?)
What is Java and Development Environment (MAC)
What is a class in Java language (2 /?)
What is the main method in Java?
What is Cubby
What is Docker?
What is null? ]
What is Keycloak
What is maven?
What is Jackson?
What is Docker
What is self
What is Jenkins
What is ArgumentMatcher?
Java thread processing
What is params
What is SLF4J?
What is Facade? ??
What is Gradle?
What is POJO
What is centOS
What is RubyGem?
What is programming?
What is before_action?
What is Docker
What is Byte?
What is Tomcat
What is the Java Servlet / JSP MVC model?
What is installed with yum / dnf group install
What is the volatile modifier for Java variables?
Install java with Homebrew
What is Maven Assembly?
What is `docker-compose up`?