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

An example analysis of the operation of adding 1 to variables by two threads of java

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "java two threads to add 1 operation example analysis of variables", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn the "java two threads of variables plus 1 operation example analysis" bar!

1mi-wrong conventional way of writing

Public static int iSmith0: public static void add () {iposit 1; action ();} public static void action () {System.out.println ("= >" + Thread.currentThread (). GetName () + ":" + I);} public static void main (String [] args) throws InterruptedException {Thread T1 = new Thread (SysUserServiceImpl::add, "T1"); Thread T2 = new Thread (SysUserServiceImpl::add, "T2"); t1.start (); t2.start ();} run result = = > = > t1purl = > T2

= = > t _ 1 _ 1 _

= = > t _ 1v _ 2v = > t _ 2VR _ 2

The result of each run is inconsistent. In the multithreaded environment, T1 performs + 1 operation on the I in the shared memory, but does not refresh the value to the main memory. At this time, T2 also performs the + 1 operation on whether I take or 0, so that the final result I is 1. Similarly, T1 processing is 1 and T2 is 2. The results of multiple runs are inconsistent.

Improved method 1-synchronous lock

Public class ThreadException {public static volatile int iTuno; public static void add () {synchronized (ThreadException.class) {iposit 1; action ();}} public static void action () {System.out.println ("= = >" + Thread.currentThread () .getName () + ":" + I) } public static void main (String [] args) throws InterruptedException {Thread T1 = new Thread (ThreadException::add, "T1"); Thread T2 = new Thread (ThreadException::add, "T2"); t1.start (); t2.start ()

}}

Pros: easy to implement

Disadvantages: large granularity of locking, low performance, distributed environment, multiple JVM conditions, synchronized failure, synchronized is only a local lock, locking only objects under the current jvm, in distributed scenarios, distributed locks should be used.

Improved method 2 AtomicInteger

Public class ThreadException {private static AtomicInteger num = new AtomicInteger (0); public static void add () {int I = num.getAndIncrement (); action (I);} public static void action (int I) {System.out.println ("by" + I + "= = >" + Thread.currentThread (). GetName () + ":" + num) } public static void main (String [] args) throws InterruptedException {Thread T1 = new Thread (ThreadException::add, "T1"); Thread T2 = new Thread (ThreadException::add, "T2"); t1.start (); t2.start ();}}

Improved method 3 lock

Public class ThreadException {public static volatile int iTuno; public static void action () {System.out.println ("= = >" + Thread.currentThread (). GetName () + ":" + I);}

Static Lock lock=new ReentrantLock (); public static void inc () {lock.lock (); try {Thread.sleep (1); iConstruction1; action ();} catch (InterruptedException e) {e.printStackTrace ();} finally {lock.unlock () } public static void main (String [] args) throws InterruptedException {Thread T1 = new Thread (ThreadException::inc, "T1"); Thread T2 = new Thread (ThreadException::inc, "T2"); t1.start (); t2.start ();}}

Distributed lock: to ensure that multiple nodes execute synchronously: 1. Based on database, 2. Based on redis cache, 3. Based on zookeeper

At this point, I believe you have a deeper understanding of the "java two threads to add 1 operation instance analysis of variables", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Internet Technology

Wechat

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

12
Report