Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1522864
  • 博文数量: 399
  • 博客积分: 8508
  • 博客等级: 中将
  • 技术积分: 5302
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-14 09:28
个人简介

能力强的人善于解决问题,有智慧的人善于绕过问题。 区别很微妙,小心谨慎做后者。

文章分类

全部博文(399)

文章存档

2018年(3)

2017年(1)

2016年(1)

2015年(69)

2013年(14)

2012年(17)

2011年(12)

2010年(189)

2009年(93)

分类: 架构设计与优化

2015-05-01 15:58:03

首先推荐使用 JVM 友好的 trove  集合系统,performance比较高。

http://www.ibm.com/developerworks/cn/java/j-perf09284.html
但是线程安全的高并发场合,可能还要使用JDK自带并发容器。

Blocking lists, using the LinkedBlockingDeque class =>
takeFirst() and takeLast(): These methods return the first and the last element of the list respectively. They remove the returned element from the list. If the list is empty, these methods block the thread until there are elements in the list.

getFirst() and getLast(): These methods return the first and last element from the list respectively. They don't remove the returned element from the list. If the list is empty, these methods throw a NoSuchElementExcpetion exception.

peek(), peekFirst(), and peekLast(): These methods return the first and the last element of the list respectively. They don't remove the returned element from the list. If the list is empty, these methods return a null value.

poll(), pollFirst(), and pollLast(): These methods return the first and the last element of the list respectively. They remove the returned element form the list. If the list is empty, these methods return a null value.

add(), addFirst(), addLast(): These methods add an element in the first and the last position respectively. If the list is full (you have created it with a fixed capacity), these methods throw an IllegalStateException exception.

Blocking lists to be used with producers and consumers of data, using the LinkedTransferQueue class =>


Blocking lists that order their elements by priority, with the PriorityBlockingQueue =>
元素必须实现 comparable 接口 的 compareTo()方法
@Override
  public int compareTo(Event e) {
    if (this.priority>e.getPriority()) {
      return -1;
    } else if (this.priority
      return 1;
    } else {
      return 0;
}
clear(): removes all the elements of the queue.
take(): returns and removes the first element of the queue. If the queue is empty, the method blocks its thread until the queue has elements.
put(E e): E is the class used to parameterize the PriorityBlockingQueue class. This method inserts the element passed as a parameter into the queue.
peek(): returns the first element of the queue, but doesn't remove it.

Blocking lists with delayed elements, using the DelayQueue class

Non-blocking lists, using the ConcurrentLinkedDeque class    =>
pollFirst()  returns and removes the first element of the list
poolLast()  returns and removes the last element of the list
getFirst() / getLast() : 并不删除元素,但列表为空时会抛异常 NoSuchElementExcpetion
peek() / peekFirst() / peekLast() :  并不删除元素,但列表为空时只会返回null

Non-blocking navigable maps, using the ConcurrentSkipListMap class =>

headMap(K toKey): K is the class of the key values used in the parameterization of the ConcurrentSkipListMap object. This method returns a submap of the first elements of the map with the elements that have a key smaller than the one passed as parameter

tailMap(K fromKey): K is the class of the key values used in the parameterization of the ConcurrentSkipListMap object. This method returns a submap of the last elements of the map with the elements that have a key greater than the one passed as parameter

putIfAbsent(K key, V Value): This method inserts the value specified as a parameter with the key specified as parameter if the key doesn't exist in the map.

pollLastEntry(): This method returns and removes a Map.Entry object with the last element of the map.

replace(K key, V Value): This method replaces the value associated with the key specified as parameter if this key exists in the map.

Random numbers, using the ThreadLocalRandom class => 示例如下
public class TaskLocalRandom implements Runnable {
public TaskLocalRandom() {
   ThreadLocalRandom.current();
}
@Override
          public void run() {
          for (int i=0; i<10; i++){
             System.out.printf("%s: %d\n",name,ThreadLocalRandom.current().nextInt(10));
             String name=Thread.currentThread().getName();
       }
}
}

Atomic variables, using the AtomicLong and AtomicIntegerArray classes
后者的使用场合在于,多个操作同一数组的不同元素,可能会形成死锁等问题。
Deadlock: This situation occurs when a thread is blocked waiting for a lock that is locked by other threads and will never free it. This situation blocks the program, so it will never finish.
If only one thread is accessing the shared object, it has to execute the code necessary to get and release the lock.

To provide a better performance to this situation, the compare-and-swap operation was developed. This operation implements the modification of the value of a variable in the following three steps:
1. You get the value of the variable, which is the old value of the variable.
2. You change the value of the variable in a temporal variable, which is the new value of the variable.
3. You substitute the old value with the new value, if the old value is equal to the actual value of the variable. The old value may be different from the actual value if another thread has changed the value of the variable.
With this mechanism, you don't need to use any synchronization mechanism, so you avoid deadlocks and you obtain a better performance.


Java implements this mechanism in the atomic variables. These variables provide the compareAndSet() method that is an implementation of the compare-and-swap operation and other methods based on it.





阅读(1920) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~