Get rid of java Myth No. 3: The atomic operations are thread-safe,

  Java threads in the atomic operation is the safety argument has often been mentioned.    By definition, atomic operation will not be interrupted in the operation, was considered to be thread safe.    In fact there are some threads atomic operation is not necessarily safe. 

  The reasons for this problem is to minimize the synchronization keyword in the code.    Synchronization will damage performance, although the losses vary by JVM.    In addition, modern JVM, synchronized performance is gradually improving.    Nevertheless, there is still the use of synchronous performance cost, and programmers will always be to improve the efficiency of their code, so this problem on the renewal of it. 

  In java, 32 or less median assignment is atoms.    In a 32-bit hardware platform, in addition to long-double and the other primitive types are usually carried out that the use of 32-bit, and usually use long double and 64 said.    In addition, the object reference guide for the use of the realization of the usually 32.    32 of these types of operations are atomic. 

  These primitive type normally used 32 or 64 that it also introduced another small Myth: the size of the original type of language is guaranteed.    This is wrong.    Java language guarantee is the original type of table in a few areas, not JVM memory size.    Therefore, int type always have the same number of the table.    In a JVM on the possible use of 32 materialize, and in another JVM may be on the 64.    Here once again stressed: all platforms is guaranteed by several of the table, 32, as well as a smaller value of the operation is atoms. 

  Well, atomic operation under what circumstances is not thread-safe?    The main point is that they may indeed thread-safe, but it has not been guaranteed!    Java threads allow threads in their own memory area preserve a copy of variables.    Allow the use of thread local private copy of each work rather than use the value of the deposit is to improve performance.    Consider the following categories: 

  Class RealTimeClock 
  ( 
  Private int clkID; 
  Public int clockID () 
  ( 
  Return clkID; 
  ) 
  Public void setClockID (int id) 
  ( 
  ClkID = id; 
  ) 
  //… 
  ) 

  RealTimeClock now considered an example of two threads at the same time and call setClockID and clockID and in the following sequence of events: 

  T1 call setClockID (5) 
  T1 will be 5 Add to their own private working memory 
  T2 call setClockID (10) 
  T2 10 Add to work their own private memory 
  T1 clockID call, it returns 5 
  5 from T1 is the return of private working memory 

  ClockI should call on the return of 10, because it is set up by T2, but the return of five because of the read-write operation of private working memory rather than the main deposit.    Assignment is of course atoms, but because JVM allow such behaviour, thread-safe is not necessarily the same time, such an act JVM is not being guaranteed. 

  Two threads have their own private copy of the agreement and without.    If such behaviour, then the private plane of the main variables and must be consistent in the following two conditions: 

  1, the use of volatile variable statement 
  2, the visit was a synchronous method, or variable synchronous block 

  If the statement volatile variables in each city and the visit of the same.    This consistency is assured by the java language, and the atom, even if the value is 64.    (Note that many of the realization of the right JVM not volatile keyword. Www.javasoft.com You can find more information.) Also, if the synchronization method or variable synchronization block is visited, and when the method or block access to the entrance Lock and methods or block the release from the lock variables simultaneously. 
  The use of any method can be guaranteed a return of 10 ClockID, which is the correct value.    Variable frequency of visiting different, your choice of different properties.    If you do a lot of variables, then the use of volatile than the use of synchronization more slowly.    Remember, if the variable declaration to be volatile, so each visit will be consistent and main memory.    By contrast, the use of synchronization, the only variable was in the lock and release the lock and the time of the same.    But synchronous code have made less complicated. 

  If you do a lot of variables and do not want to have to visit each and all of the sync loss or because you want to rule out other causes can be complicated when considering the use of synchronization. 

Bookmark it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Google
  • DotNetKicks
  • DZone
  • Furl
  • Netvouz

Releated Articles

  • Popuklar Articles

0 Comments to “Get rid of java Myth No. 3: The atomic operations are thread-safe,”

No Comments. Send your comment.

Leave a Reply

You must be logged in to post a comment.