Java Notes-8

- 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值