Performance tuning for Java concurrency

Source: Internet
Author: User

1. Mutex Technology
    • Synchronized
    • Lock
    • Atomic

Performance comparison atomic > Lock > Synchronized, of course this is not absolute. When the number of threads is less, the efficiency of synchronized is considerable, and the code block with this key modifier is very readable. So we first consider the use of synchronized when programming, and consider using lock or atomic when the performance requirements for concurrency are high. Atomic is suitable for a simple object, and is not recommended if the object is more complex.

2. Lock-Free container
    • Copyonwritearraylist
    • Copyonwritearrayset
    • Concurrenthashmap
    • Concurrentlinkedqueue

When introducing the Mian lock container, first look at the cow (copy on write) technology. The technique used by the object is generally similar to an array of data formats such as larger storage volumes. When using the cow technique to write to an array, a copy of the original is first created, and then the copy is written. When the write operation completes, the reference is pointed to the modified copy by an atomic operation. This write operation is mutually exclusive, so only one thread is written to the array at a time.

The introduction of cow technology into the container is called a lock-free container, such as copyonwritearraylist. In a lock-free container, read and write operations on the data can occur simultaneously, reducing the acquisition and release of Locks. One thing to note, though, is that the value of the original data is always read.

Lock-free containers generally used in the scene is a small amount of data, read more times, write fewer times.

3.ReadWriteLock

The lock is used for less write operations and more read operations. When multiple readers are reading, no conflict occurs. The write operation cannot be performed when the read lock is held. When a write lock is held by another task, no reader can read until the write lock is released.

1  PackageCom.dy.xidian;2 3 Importjava.util.ArrayList;4 Importjava.util.Collections;5 ImportJava.util.Random;6 ImportJava.util.concurrent.ExecutorService;7 Importjava.util.concurrent.Executors;8 ImportJava.util.concurrent.TimeUnit;9 ImportJava.util.concurrent.locks.Lock;Ten ImportJava.util.concurrent.locks.ReentrantReadWriteLock; One  A  Public classReaderwriterlist<t> { -     PrivateArraylist<t>lockedlist; -     PrivateReentrantreadwritelock lock =NewReentrantreadwritelock (true); the      PublicReaderwriterlist (intsize, T initialvalue) { -Lockedlist =NewArraylist<t>(collections.ncopies (Size, initialvalue)); -     } -  +      PublicT Set (intindex, T Element) { -Lock Wlock =Lock.writelock (); + Wlock.lock (); A         Try { at             returnlockedlist.set (index, Element); -}finally { - Wlock.unlock (); -         } -     } -  in      PublicT Get (intindex) { -Lock Rlock =Lock.readlock (); to Rlock.lock (); +         Try { -             if(Lock.getreadlockcount () > 1) the System.out.println (Lock.getreadlockcount ()); *             returnLockedlist.get (index); $}finally {Panax Notoginseng lock.readlock (). Unlock (); -         } the     } +  A      Public Static voidMain (string[] args) { the         NewReaderwriterlisttest (30, 1); +     } - } $  $ classReaderwriterlisttest { -Executorservice exec =Executors.newcachedthreadpool (); -     Private Final Static intSIZE = 100; the     Private StaticRandom Rand =NewRandom (47); -     Privatereaderwriterlist<integer> list =NewReaderwriterlist<integer>(WuyiSIZE, 0); the  -     Private classWriterImplementsRunnable { Wu  - @Override About          Public voidrun () { $             Try { -                  for(inti = 0; I < 20; i++) { - List.set (i, Rand.nextint ()); -TimeUnit.MILLISECONDS.sleep (100); A                 } +}Catch(interruptedexception e) { theSystem.out.println ("interrupted!"); -             } $System.out.println ("Writer finished, shutting down"); the         } the     } the  the     Private classReaderImplementsRunnable { -  in @Override the          Public voidrun () { the             Try { About                  while(!thread.interrupted ()) { the                      for(inti = 0; i < SIZE; i++) { the List.get (i); theTimeUnit.MILLISECONDS.sleep (1); +                     } -                 } the}Catch(interruptedexception e) {BayiSystem.out.println ("Read interrupted"); the             } the         } -     } -  the      PublicReaderwriterlisttest (intReadersintwriters) { the          for(inti = 0; i < readers; i++) theExec.execute (NewReader ()); the          for(inti = 0; I < writers; i++) -Exec.execute (NewWriter ()); the     } the}

Performance tuning for Java concurrency

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.