views:

90

answers:

2

how atomic ways can make our codes thread-safe?

+1  A: 

You might want to read up a little on concurrency and locking in Java. Also, check out this other SO thread on Java memory model/concurrency.

Jess
+1  A: 

Atomic operations are operations that cannot be interrupted at all.

The typical synchronization problems occur when one thread updates a data structure not guarded by a synchronization mechanism, so threads can read stale or inconsistent values, because the values are being changed from beneath their feet by the other threads.

The mechanism to avoid this problem is to synchronize access to the data structures, so you put an order and ensure a single thread completes its use of the data structure before some other thread access it.

These synchronization mechanisms to be able to work have to make sure that they themselves cannot be scheduled out of the CPU while they are operating, because if that happens then a thread might get to update the structure when it's not supposed to.

These mechanisms are implemented in terms of these atomic operations to ensure they work. For example, semaphores rely on having atomic increment and 'test and decrement' operators.

This is a huge subject, usually covered under the subject 'distributed systems', this term might lead you to better resources to understand concurrency.

Vinko Vrsalovic