In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you how to understand the thread safety issues in Java. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
Thread safety is a relatively profound problem, and it is a difficult technical difficulty for many programmers to master. If a programmer has a good grasp of threads, then the internal skills of this programmer are quite good.
Here I would like to talk about some of my personal opinions on how to ensure thread safety in java. I hope it will be helpful to you. If there is something wrong there, please give me your advice.
Thread safety problems mainly occur when accessing critical resources, that is, when accessing the same object, there may be irreparable losses, especially when it comes to financial security, and, of course, database transactions. They are very similar, all to ensure the atomicity of the data.
So how to ensure thread safety in java?
Locking objects that are shared with me means that when I use it, you have to wait until I finish using it, and vice versa. It's like going to the bathroom. I can't go when you go.
How to add the lock? Here are three ways to lock
First, take a look at the example code.
Public class TraditionalSynchornizedTest {/ * @ param args * / public static void main (String [] args) {new TraditonalSynchornizedTest () .sartThread ();} public void sartThread () {final Outerput outerput = new Outerput (); new Thread (new Runnable () {@ Override public void run () {while (true) {try {Thread.sleep (5) } catch (InterruptedException e) {e.printStackTrace ();} outerput.print ("zhangsanfeng");}) .start (); new Thread (new Runnable () {@ Override public void run () {while (true) {try {Thread.sleep (5);} catch (InterruptedException e) {e.printStackTrace () } outerput.print ("luxiaofeng");}. Start ();} public class Outerput {public void print (String name) {for (int I = 0 System.out.print I < name.length (); iTunes +) {System.out.print (name.charAt (I));} System.out.println ();}
The above code does not lock the jointly held object outerput, so there will be thread safety problems
1. Lock the code block
Locking a jointly held object allows you to write an inner class like this
Public class Outerput {public void print (String name) {synchronized (this) {for (int I = 0polii < name.length (); iTunes +) {System.out.print (name.charAt (I));} System.out.println ();}
2. Lock the non-static method, and the locked object is this.
Public class Outerput {public synchronized void print (String name) {for (int I = 0 I < name.length (); iTunes +) {System.out.print (name.charAt (I));} System.out.println ();}}
3. Who is the object that locks the static method?
Public static synchronized void print2 (String name) {for (int I = 0 I < name.length (); iTunes +) {System.out.print (name.charAt (I));} System.out.println ();}
In fact, the object that is locked is the bytecode object, Outerput.class
If you hold the same object as a non-static method, you can hold the same bytecode object.
The above is how to understand the thread safety issues in Java shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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.