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 method of verifying iTunes + by java

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "what is the method of java verification i++". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian slowly and deeply to study and learn "what is the method of java verification i++" together!

i++ thread unsafe

i++ is a statement that we have been writing since we started learning to write code. There are often predecessors who have said that i++ is thread unsafe. Today, we will simulate multiple threads to perform ++ operations on i at the same time.

The code is as follows:

import java.util.concurrent.CountDownLatch;

public class IDemo {

public static void main(String[] args) {//Counts with CountDownLatch, which has two main methods// 1 is await, which suspends the current thread// 2 is countDown, which decrements the current counter by 1. If the counter decrements to 0, the suspended thread can continue executing CountDownLatch latch = new CountDownLatch(1);// new A class with i = 0 TestCounter counter = new TestCounter();//defines thread operations, first call the await method of latch to wait for the counter to become 0//and then execute the counter.add method, let i++; Runnable runnable = () -> {try { latch.await(); counter.add(); } catch (InterruptedException e) { e.printStackTrace(); } };//loop 1000 times, create 1000 threads for (int i = 0; i < 1000; i++) { Thread t = new Thread(runnable); t.start(); }//Call the latch.countDown method here to decrement the counter to 0, so that all suspended threads can execute try { latch.countDown();//main thread pauses for 3 seconds to allow child threads to finish executing Thread.sleep(3000L); } catch (InterruptedException e) { e.printStackTrace(); }//Output the value of i after the last execution of i++ System.out.println(counter.i);

}

/** * Encapsulate i and i++ operations using inner classes */static class TestCounter {int i = 0;

void add() { i++; }

}}

The output is as follows:

Multiple runs will have different results

Thank you for reading, the above is "java verification i++ method is what" the content, after the study of this article, I believe that we have a deeper understanding of java verification i++ method is what this problem, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

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