Java Notes-8

来源:互联网 发布:利用淘宝双十一赚钱 编辑:程序博客网 时间:2024/05/19 05:03
- The  Lock class provides for exclusive
access by the owner of the lock by allowing only one party to hold the lock at a time

through the  lock() and  unlock() methods. 


-In addition to the standard-looking  lock() method, the  Lock interface has  tryLock()
methods that do not block or that block for a specified period of time in order to acquire
the lock.

-The  ReadWriteLock interface is a gateway to two different locks, one for reading and
one for writing. The idea behind read/write locks is that for most resources it is OK for
many “readers” to be viewing data, as long as it is not changing. Conversely, a writer of
the data generally requires exclusive access to it. This is just what read/write locks do.


-. The  Condition in‐
terface represents this functionality with its  await() ,  signal() , and  signalAll()
methods


-The  CountDownLatch is a very simple synchronization utility that allows any number of
threads to block, waiting for a countdown value to reach  0 before being “released” to
continue their activities. 


-Semaphores are a very old synchronization construct that has been used in many other
languages. Conceptually, a semaphore is a pool of permits—intangible permission slips
to perform some activity.

-Calling  acquire() when no permits are available causes the caller to block
until one is released. In this way, for example, a semaphore could be used to limit access
to some resource to a specified number of threads


-In this code snippet,  readData() effectively limits itself to five concurrent reading
threads at any given time.

-The  java.util.concurrent.atomic package holds an interesting set of wrapper classes
for atomic, “all-or-nothing” operations on certain primitive types and reference values.


AtomicBoolean.java
AtomicInteger.java
AtomicIntegerArray.java
AtomicLong.java
AtomicLongArray.java
AtomicReference.java
AtomicReferenceArray.java



-The  compareAndSet() method first performs a comparison to an expected value
( true or  false in the case of a Boolean) and only if the value matches does it assign the
new value. The interesting thing is that both of these operations happen “atomically,”
together. 

-The  compareAndSet() method has a strange twin named  weakCompareAndSet() , which
has the dubious distinction that it simply may not work when called. It is, however, nice
enough to tell you when it doesn’t work by returning  false . What’s the point of this?
Well, by allowing this fuzziness, Java may be able to make the implementation of the
weak method much faster than the “certain” one. You can loop and retry the weak
method instead and it may improve performance on some architectures. 



-trim() is a useful method that removes leading and trailing whitespace (i.e., carriage
return, newline, and tab) from the  String





-In contrast to the immutable string, the  java.lang.StringBuilder class is a modifiable
and expandable buffer for characters. You can use it to create a big string efficiently.
StringBuilder and  StringBuffer are twins; they have exactly the same API. 



0 0
原创粉丝点击