-2

how atomic ways can make our codes thread-safe?

Johanna
  • 27,036
  • 42
  • 89
  • 117
  • 1
    You might want to clarify your question a bit ... "atomic ways" is not very specific or clear. – Beep beep Aug 05 '09 at 15:33
  • this is a very vauge and hard to understand question. what is an "atomic way"? maybe you mean atomic method? or synchronized method? – Peter Recore Aug 05 '09 at 18:51

2 Answers2

1

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.

Community
  • 1
  • 1
Beep beep
  • 18,873
  • 12
  • 63
  • 78
1

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
  • 330,807
  • 53
  • 334
  • 373