Java Concurrency: Thread Safety Analysis

What is java thread-safe:
Is the thread synchronization means is that when a program on a thread-safe method of access or statements, other he can no longer operate, and must wait until after the end of the visit in order to access to the thread-safe method
What is thread-safe:
If your code where the process has multiple threads running at the same time, and these threads may run this code at the same time. If the result of each run single-threaded operating results and the same, and also the values ​​of other variables and expectations are the same, that is thread-safe. 
Or: the interface of a class or program provided for switching between threads is an atomic operation or multiple threads does not result in the presence of ambiguous results of the implementation of the interface, which means that we do not consider the issue of synchronization.
Thread safety problems are caused by the global variables and static variables.
If each thread to global variables, static variables only read, no write operation, in general, the global variable is thread-safe; if multiple threads simultaneously perform write operations generally need to consider thread synchronization, or else which may affect the thread safety.
Read the vector source of many of his students will know the operation is the addition of synchronized modified to add elements such as his. (I do not know what is the meaning of self-synchronized Baidu!)
 
1
2
3
public synchronized void addElement(E obj) {  modCount++;
       ensureCapacityHelper(elementCount + 1);  elementData[elementCount++] = obj;
}
 
And all operations plus synchronized HashMap are not modified, as he put source
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public V put(K key, V value) {
     if (key == null)
         return
      putForNullKey(value);
      int hash = hash(key.hashCode());
      int i = indexFor(hash, table.length);
      for(Entry<K,V> e = table[i]; e != null; e = e.next) {
         Object k;
         if (e.hash == hash &&((k = e.key) == key || key.equals(k))) {
             V oldValue = e.value;
             e.value = value;
             e.recordAccess(this);
             return
             oldValue;    }
     }
     modCount++;
     addEntry(hash, key, value, i);
     return null;
 }
 
Look at the source code add methods of ArrayList
 
1
2
3
4
5
public boolean add(E e) {
     ensureCapacity(size + 1);  // Increments modCount!!
     elementData[size++] = e;
     return true;
 }
 
StringBuffer append look at the source code, he is synchronized modified
 
1
2
3
4
5
public synchronized
  StringBuffer append(String str) {
     super.append(str);
     return this;
 }
 
Finally Properties of setProperty method, he is synchronized modified
 
1
2
3
4
5
public synchronized
  Object setProperty(String key, String value) {
      return
      put(key, value);
 }
 
Thus it can determine who is thread-safe
 
 
The so-called volatile measure, that is,
1. Each value from memory, not something to take the value from the cache. This ensures a volatile modification of shared variables, each of the update for other threads are visible.
2. volatile to ensure the immediate visibility of the other threads, there is no guarantee atomicity.
3. Because sometimes volatile operation will not be saved, description will not cause obstruction. Unavailable counter with a multithreaded environment
 
Once a shared variable (member variables of the class, the class of static member variables) after being declared volatile, then have the two semantics:
1) to ensure visibility when different threads to this variable operation, that is a thread modifies the value of a variable, this new value to other thread is immediately visible.
2) prohibition command reordering.
volatile only to provide a guarantee to access the variable, every time read the latest value from memory, and will not be used to cache the value of the register - every time read from memory.
And the modification variable, volatile does not provide a guarantee atomicity.
Since the update is likely to lead to another thread access to the latest value of the variable, not out of the loop
Multi-threaded lock protection must be used under the counter.

Guess you like

Origin www.cnblogs.com/ygria/p/11463423.html