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/03 Report--
What is the thread safety problem in Java language? I believe many inexperienced people don't know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
The Java language is a multithreaded language that is accomplished through synchronization (mutual exclusion) and collaboration (waiting and waking up). Let's talk about synchronization here.
Thread insecurity mainly comes from class variables (static variables) and instance variables, the former in the method area and the latter in the heap, both of which are shared areas. There is no problem with a local variable because it is in the thread's unique stack. Let's take a look at the following example:
Public class Test implements Runnable {private int j; public Test () {} public void testThreadLocal () {System.out.println (Thread.currentThread (). GetId () + ": = = begin"); j = 2; System.out.println (Thread.currentThread (). GetId () + ":" + j); j = 20 System.out.println (":" + j * 3 + ":"); System.out.println (Thread.currentThread (). GetId () + ": = = end");} public static void main (String [] args) {Test t = new Test (); for (int I = 0; I < 3000; iTunes +) {new Thread (t). Start () @ Override public void run () {testThreadLocal ();}}
Thread unsafe problems arise when executing the main method of this class. The above blue statement should be printed: 60, but after 3000 threads are actually opened (for the convenience of unsafe phenomena), the following red will appear: 6:
655 rides = end
49Gill = end
: 6:
156villa = end
152:2
: 60:
Modify the main method to use multiple Test objects, and the result is the same.
Public static void main (String [] args) {Test t = new Test (); for (int I = 0; I < 3000; iTunes +) {new Thread (new Test ()) .start ();}}
We keep the practice of multiple Test objects and add a synchronization keyword to the testThreadLocal method.
Public synchronized void testThreadLocal ()
As a result, it is useless and it is still not safe. Change it to a Test object, and that's fine. The reason is simple: synchronized achieves thread safety by placing locks on objects. When using multiple Test objects, it is not possible to simply lock the this object, only on the class (in java, the class is still represented by a special Class object). So change it to:
Public void testThreadLocal () {synchronized (this.getClass ()) {System.out.println (Thread.currentThread (). GetId () + ": = = begin"); j = 2; System.out.println (Thread.currentThread (). GetId () + ":" + j); j = 20 System.out.println (":" + j * 3 + ":"); System.out.println (Thread.currentThread () .getId () + ": = = end");}}
That's all right. Let's take a look at the use of class variables, first remove the synchronized keyword, restore to the original code, and then change the instance variables to class variables.
Private int j; private static int j
The experimental results are basically the same as using instance variables, but the difference is that we can lock the class in this way. Note that the testThreadLocal method is changed to a static method.
Public synchronized static void testThreadLocal () {System.out.println (Thread.currentThread (). GetId () + ": = = begin"); j = 2; System.out.println (Thread.currentThread (). GetId () + ":" + j); j = 20; System.out.println (":" + j * 3 + ":") System.out.println (Thread.currentThread () .getId () + ": = = end");}
As you can see from the above example, we have to be very careful when using class variables and instance variables. In a multithreaded environment, it is easy to be thread-unsafe. Above we only take the basic type int as an example, if it is other complex types, or even the basic data type like long, which requires two atomic operations when assigning values, the thread unsafe situation is more secret.
After reading the above, have you mastered the methods of thread safety problems in Java language? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.