public static Object getLast(Vector list) { synchronized(list) { int lastIndex = list.size() - 1; return list.get(lastIndex); } }
好比在迭代的時候,須要加鎖,那就會嚴重下降併發。這時候也能夠考慮經過「克隆」容器的方法。經過考慮容器的大小、操做的開銷等因素來抉擇。html
The iterators returned by the iterator method of the collections
* returned by all of this class's "collection view methods" are
* fail-fast: if the Hashtable is structurally modified at any time
* after the iterator is created, in any way except through the iterator's own
* remove method, the iterator will throw a
* ConcurrentModificationException
Hashtable
has two parameters that affect its performance: initial capacity and load factor. Runnable
to an Executor
happen-before its execution begins. Similarly for Callables
submitted to an ExecutorService
.Future
happen-before actions subsequent to the retrieval of the result via Future.get()
in another thread.Lock.unlock
, Semaphore.release
, and CountDownLatch.countDown
happen-before actions subsequent to a successful "acquiring" method such as Lock.lock
, Semaphore.acquire
,Condition.await
, and CountDownLatch.await
on the same synchronizer object in another thread.Exchanger
, actions prior to the exchange()
in each thread happen-before those subsequent to the corresponding exchange()
in another thread.CyclicBarrier.await
and Phaser.awaitAdvance
(as well as its variants) happen-before actions performed by the barrier action, and actions performed by the barrier action happen-before actions subsequent to a successful return from the corresponding await
in other threads.