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 ArrayList by java

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

Share

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

This article mainly explains the "java verification ArrayList method is what", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's train of thought slowly in-depth, together to study and learn "java verification ArrayList method is what" it!

Verify that the thread of ArrayList is not safe

Verify the ArrayList code as follows:

Import java.util.ArrayList;import java.util.concurrent.CountDownLatch

Public class IDemo {

Public static void main (String [] args) {/ / uses CountDownLatch to count. There are two main methods / / 1 is await, so that the current thread pauses / / 2 is countDown, minus the current counter by 1. If the counter is reduced to 0, the paused thread can continue to execute CountDownLatch latch = new CountDownLatch (1); / / new has an ArrayList ArrayList list = new ArrayList (10) / / Thread operation is defined. First call the await method of latch to wait for the counter to change to 0 / / and then execute the list.add method; Runnable runnable = ()-> {try {latch.await (); list.add (new Object ());} catch (InterruptedException e) {e.printStackTrace () }}; / / cycle 1000 times to create 1000 new thread for (int I = 0; I < 1000; iThread +) {Thread t = new Thread (runnable); t.start () } / / call the latch.countDown method here to reduce the counter to 0, then all paused threads can execute try {latch.countDown (); / / the main thread pauses for 3 seconds so that the child thread can finish executing Thread.sleep (3000L);} catch (InterruptedException e) {e.printStackTrace () } / / output the number of System.out.println (list.size ()) in the arrayList after the final execution

}} Thank you for your reading, the above is the content of "what is the method of java verifying ArrayList". After the study of this article, I believe you have a deeper understanding of what the method of java verifies ArrayList 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

Internet Technology

Wechat

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

12
Report