简体   繁体   中英

Under what circumstances do you need to synchronize an array in Java?

Under what circumstances do you need to synchronize an array?

My thoughts are, do you need to synchronize for access? Say two threads access the array at the same time, is that going to crash?

What if one edits, while one is reading? (separate values, and the same in different circumstances)

Both editing different things?

Or is there no JVM crash like for arrays when you don't synchronize?

Under what circumstances do you need to synchronize an array?

It's sort of you either always need to or never need to. Like @EJP said, he's never done it because there's almost always a better data structure than an array , anyway (edit: there are lots of good use cases for arrays, but they're almost always used in isolation. eg ArrayList). But if you insist on sharing arrays between threads, array elements aren't volatile, so because of possible caching, you'll get inconsistencies and corrupt data without using synchronized .

My thoughts are, do you need to synchronize for access? Say two threads access the array at the same time, is that going to crash?

Crash, no, but your data could be inconsistent, and extra inconsistent if they're 64-bits on a 32-bit architecture.

What if one edits, while one is reading? (separate values, and the same in different circumstances)

Please don't. Wrapping your head around the Java memory model is hard enough. If you haven't established that a read or a write happened-before another read or write, the ultimate sequencing is undefined.

This is a difficult question because it touches on a lot of Concurrency topics.

First I'd start with, http://docs.oracle.com/javase/tutorial/essential/concurrency/sync.html

Threads communicate primarily by sharing access to fields and the objects reference fields refer to. This form of communication is extremely efficient, but makes two kinds of errors possible: thread interference and memory consistency errors. The tool needed to prevent these errors is synchronization .

A. Thread Interference describes how errors are introduced when multiple threads access shared data.

B. Memory Consistency Errors describes errors that result from inconsistent views of shared memory.

So to answer the main question directly, You synchronize an array when you believe that your array maybe be accessed in a way that introduces Thread interference or Memory Consistency Errors mainly.

You end up with what's called a Race Condition . Whether that crashes your application or not depends on your application.

So if you do not synchronize access to an array that is shared between multiple threads you run the chance of threads interleaving modifications to this array ( ie. Thread Interference ). Or the chance that threads read inconsistent data in your array ( ie. Memory Consistency ).

The solution is typically to synchronize the array, or us a Collection built for Concurrency, such as those discribed at https://docs.oracle.com/javase/tutorial/essential/concurrency/collections.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM