Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the way to implement Java multithread synchronization?

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the way to achieve Java multithread synchronization". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the way of Java multithread synchronization?"

Give me a lock. I can create a rule.

As many people know, there is an important keyword in Java multithreaded programming, synchronized. But many people will be confused when they see this thing: "it is said that the synchronization mechanism is implemented through object locking, but with such a keyword, I can't see which object is locked by the Java program."

Yes, I was confused and confused about this question at first. Fortunately, however, we have the following routine:

Public class ThreadTest extends Thread {private int threadNo; public ThreadTest (int threadNo) {this.threadNo = threadNo;} public static void main (String [] args) throws Exception {for (int I = 1; I < 10; iTunes +) {new ThreadTest (I) .start (); Thread.sleep (1);}} @ Override public synchronized void run () {for (int I = 1; I < 10000; iSense +) {System.out.println ("No." + threadNo + ":" + I) }

This program actually allows 10 threads to count on the console, from 1 to 9999. Ideally, we want to see one thread finish counting, and then another thread starts counting. But the execution of this program tells us that these threads are still scrambling to count, and there are no rules to speak of.

But careful readers have noticed that the run method still adds a synchronized keyword, and it makes sense that these threads should be able to execute the run method one by one.

But as we mentioned in the previous article, adding the synchronized keyword to a member method actually takes the object in which the member method is located as the object lock. In this case, it takes a concrete object of the ThreadTest class, that is, the thread itself, as the object lock. There are ten threads, each of which holds the object lock of its own thread object. This must not have the effect of synchronization. In other words, if you want to synchronize these threads, then the object locks held by these threads should be shared and *!

Let's take a look at the following routine:

Public class ThreadTest2 extends Thread {private int threadNo; private String lock; public ThreadTest2 (int threadNo, String lock) {this.threadNo = threadNo; this.lock = lock;} public static void main (String [] args) throws Exception {String lock = new String ("lock"); for (int I = 1; I < 10; iTunes +) {new ThreadTest2 (I, lock). Start (); Thread.sleep (1) }} public void run () {synchronized (lock) {for (int I = 1; I < 10000; iTunes +) {System.out.println ("No." + threadNo + ":" + I);}

We notice that the program creates an object of type String before the main method starts 10 threads. And assign this object to the private variable lock in each ThreadTest2 thread object through the constructor of ThreadTest2. According to the value passing characteristics of the Java method, we know that the lock variables of these threads actually point to the same area in heap memory, that is, the area where the lock variables in the main function are stored.

The program removes the synchronized keyword before the original run method and uses a synchronized block in the run method to implement it. The object lock for this synchronization block is the String object created in the main method. In other words, they point to the same String type object, and the object lock is shared and *!

As a result, we see the expected effect: 10 threads are no longer scrambling to report, but one by one.

Let's take a look at the following routine:

Public class ThreadTest3 extends Thread {private int threadNo; private String lock; public ThreadTest3 (int threadNo) {this.threadNo = threadNo;} public static void main (String [] args) throws Exception {/ / String lock = new String ("lock"); for (int I = 1; I < 20; iTunes +) {new ThreadTest3 (I). Start (); Thread.sleep (1);}} public static synchronized void abc (int threadNo) {for (int I = 1; I < 10000) ) {System.out.println ("No." + threadNo + ":" + I);}} public void run () {abc (threadNo);}}

Careful readers have found that this code does not use the String object created in the main method as a thread lock for these 10 threads. Instead, thread synchronization is achieved by calling abc, a static synchronization method in the thread, in the run method. I want to see here, you should be confused: what do synchronized static methods use to make object locks here?

We know that for synchronous static methods, the object lock is the Class instance of the class in which the static release is located, because in JVM, all loaded classes have class objects of *. In this case, they are * ThreadTest3.class objects. No matter how many instances of this class we create, its class instance is still one!

So we know:

1. For a synchronized method or code block, an object lock must be obtained in order to enter the synchronous method or code block to operate.

2. If you use method-level synchronization, the object lock is the object where the method is located. If it is a static method, the object lock refers to the object where the method is located.

Class object (*)

3. For code blocks, object locks refer to abc in synchronized (abc)

4. In the case of *, there are multiple object locks for each thread, so synchronization fails. The second method shares the same object lock lock, so synchronization takes effect. Third, because it is a static, the object is locked as a class object of ThreadTest3, so synchronization takes effect.

If the above is correct, there are two ways to synchronize, synchronization blocks and synchronization methods (why not wait and notify? I will elaborate on this in the supplementary chapter)

If it is a synchronous code block, the object lock needs to be specified by the programmer. Generally, some code is synchronized (this), which only takes effect in singleton mode.

(there is one and only one instance of this class)

If it is a synchronous method, it is divided into static and non-static methods.

Static methods must be synchronized. Non-static methods only take effect in singleton mode. Static methods are recommended (don't worry about singletons).

Therefore, in Java multithreaded programming, the most common synchronized keyword actually relies on the mechanism of object locks to achieve thread synchronization.

We seem to hear synchronized saying to us, "give me a lock and I can create a rule."

Thank you for reading, the above is the content of "what is the way to achieve Java multithread synchronization". After the study of this article, I believe you have a deeper understanding of what the way of Java multithread synchronization is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report