In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article gives you an introduction to Java programming language thread safety and synchronization is how, the content is very detailed, interested friends can refer to, I hope to help you.
Java programming language is a fairly simple programming language for users. However, we still need to pay attention to related issues when using it. Below we will familiarize ourselves with thread safety issues and information synchronization of Java programming language.
In its own right, concurrent programming is a technique that provides simultaneous execution of operations, whether on a single system or distributed across a large number of systems. Such operations are actually sequences of instructions, such as subtasks of a single *** task, that can be executed in parallel, either as threads or as processes. The essential difference between threads and processes is that processes are usually independent (e.g., independent address spaces) and can only interact through the inter-process communication mechanisms provided by the system, while threads usually share state information of a single process and can directly share system resources and objects in memory.
Concurrency can be achieved through multiple processes in one of two ways. *** One approach is to run processes on the same processor and let the operating system handle context switching between processes. (Understandably, this switch is slower than context switching between multiple threads within the same process.) The second approach is to build massively parallel and complex distributed systems that run multiple processes on different physical processors.
From a built-in support perspective, the Java programming language provides concurrent programming through threads; each JVM can support many threads executing simultaneously. Threads can be created in the Java programming language in one of two ways:
Inherit java.lang.Thread class. In this case, the run() method of the overridden subclass must contain code that implements the thread runtime behavior. To execute this code, instantiate the subclass object and call the object's start() method, so that you can execute the run() method internally.
Create a custom implementation of the Runnable interface. This interface contains only a run() method, in which the application code is placed. To execute this code, instantiate the object that implements the class, and then pass the object as a constructor argument when creating a new Thread. Then call the start() method of the newly created thread object to begin executing the new thread of control.
Thread safety and synchronization
A method in a Java programming language object is said to be thread-safe if it can safely run in a multithreaded environment. To achieve this security, there must be a mechanism by which multiple threads running the same method can synchronize their operations so that only one thread is allowed to be processed when accessing the same object or line of code. This synchronization requires threads to communicate with each other using objects called signals.
One type of signal is called a mutex or mutex. As the name implies, ownership of the semaphore object is mutually exclusive, that is, only one thread can own a mutex at any given time. Other threads that want ownership are blocked and must wait until the thread that owns the mutex releases it. If multiple threads queue up sequentially for the same mutex, only one waiting thread gets it when the current owner releases it; the other threads continue to block.
In the early 1970s, C.A.R. Hoare and others developed a concept called the monitor. A monitor is a body of code whose access is protected by mutex. Any thread that wants to execute this code must get the associated mutex at the top of the code block and then release it at the bottom. Because only one thread can own a mutex at a given time, this effectively ensures that only the thread that owns it can execute the monitor's code block. (Protected code does not need to be contiguous--for example, every object in the Java programming language has a monitor associated with it.)
Any developer who wants to program threads in the Java programming language will immediately see the above as an effect of the synchronized keyword. You can ensure that Java code contained in synchronized blocks is executed by only one thread at a given time. Internally, the synchronized keyword can be translated by runtime into a situation where all competing threads are trying to get the (only) mutex associated with the object instance they are operating on. The thread that succeeds in getting the mutex runs the code and then releases the mutex when it exits the synchronized block.
Waiting and notification
The wait/notify construct also plays an important role in the Java programming language's inter-thread communication mechanism. The basic idea is that a condition required by one thread can be facilitated by another thread. The wait condition can then be satisfied. Once the condition is true, the thread that raised the condition notifies waiting for the thread to wake up and continues where it left off.
Wait/notify is harder to understand and judge than synchronized. Determining the logic of the wait/notify method requires determining the logic of all the methods that use it. Judging one method at a time, isolating it from others, is a reliable way to draw erroneous conclusions about the behavior of the overall system. Obviously, the complexity of doing so increases rapidly as the number of methods to judge grows.
About Java programming language attention thread safety and synchronization is how to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.