Thread Safe and your connection to Collections

4

What is thread safe, and what is your connection to data collections?

    
asked by anonymous 05.06.2014 / 22:36

1 answer

4

As you may know, if more than one thread accesses the same object, concurrency issues (conflicts / inconsistencies in the data that this object contains, that is, in the state of that object) / p>

To say that a class is thread safe means that an object of this class can be accessed by more than one thread without these conflicts occurring.

A thread safe collection has methods for you to access or iterate through your data without these conflicts occurring.

Examples of situations where a thread-safe collection is used:

  • In a mobile chat where you maintain a message list, a message can be added by the user, which usually occurs on the main thread, or a message can be sent via another user's network. usually occurs in a secondary thread that is waiting for incoming messages. As it is the same list, the insertion of messages should be done in a thread-safe manner.

  • Another similar example, and also for mobile, is a tracking application that maintains a list of positions captured by GPS and periodically attempts to send them to a server. The list can either save new objects (positions that GPS gets from the geo-positioning satellites) or remove objects (positions that have already been sent to the server).

  • Not directly related to a collection, but similar: The Android database is SQLite, which is not thread-safe . If you have more than one thread wanting to access it concurrently, you will need to keep a monitor / lock to prevent an exception from occurring if the database is accessed by both at the same time.

  • 05.06.2014 / 22:49