About synchronized Java (Android) arraylist

Asked 2 years ago, Updated 2 years ago, 22 views

Java (Android) is changing arraylist data from multiple threads

At the same time, data was added (.add)

So I learned about synchronized while learning about synchronized

synchronized(data) { data.add(""); }

So I changed it as above But if the phenomenon of adding data at the same time occurs

Does synchronization mean that you wait while the other thread changes?

Or I wonder if you don't access it at all and don't add data.

It's late, but happy new year!~^

^

java android

2022-09-22 13:11

1 Answers

As you said, blocks designated as synchronized will internally Lock when a thread enters the block to prevent other threads from accessing it.

Accessing only one thread at a time is guaranteed because Unlock is released when all processing is done and the block is left.

Keep in mind that when you need to access shared resources in a multi-threaded environment, you must consider it.


2022-09-22 13:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.