Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

并发编程交流

7,050 views

Published on

杭州程序员并发编程交流PPT,活动地址:
http://hangzhou.koubei.com/huodong/10610100000004750

Published in: Technology
  • Be the first to comment

并发编程交流

  1. 1. 并发编程交流<br />bluedavy<br />2010-03-19<br />
  2. 2. AboutPPT<br />对于Java体系而言主要还是借助多线程来做并发<br />so下面的一切都是多线程相关点,不涉及多进程<br />资源竞争和一致性<br />Sun JDK提供的支持<br />并发包值得学习的地方<br />尽可能Nonblocking--例子<br />线程<br />线程交互<br />线程池<br />线程跟踪和问题查找<br />线程调度<br />Some tips<br />
  3. 3. 资源竞争和一致性<br />Sun JDK提供的支持<br />Synchronized<br />Semaphore、 CountdownLatch<br />ReentrantLock、Condition<br />AtomicInteger、ConcurrentHashMap、ArrayBlockingQueue<br />
  4. 4. 资源竞争和一致性<br />并发包值得学习的地方<br />AtomicInteger系列<br />CAS<br />ConcurrentHashMap<br />拆分锁<br />Semaphore、CountdownLatch<br />并发包中最强悍的类:AbstractQueuedSynchronizer<br />基于LockSupport.park和LockSupport.unpark来操控线程是否可被调度<br />基于队列来做等待<br />看的也不是非常明白,需要懂的人分享下<br />
  5. 5. 尽可能Nonblocking例子<br />常见代码如下<br />private static Map<String,Connection> connections=new HashMap<String,Connection>();<br />public Connection get(String key) throws Exception{<br /> synchronized(connections){<br /> if(connections.containsKey(key)){<br /> return connections.get(key);<br /> }<br /> Connection conn=createConnection(key);<br />connections.put(key,conn);<br /> return conn;<br /> }<br />}<br />
  6. 6. 尽可能Nonblocking例子<br />改造想法<br />connections改为ConcurrentHashMap<br />借助putIfAbsent来减少判断是否存在的lock<br />private static ConcurrentHashMap<String,Connection> connections=new ConcurrentHashMap<String,Connection>();<br />public Connection get(String key) throws Exception{<br /> if(connections.containsKey(key)){<br /> return connections.get(key);<br />}<br />Connection conn=createConnection(key);<br />Connection realConn=connections.putIfAbsent(key,conn);<br />if(realConn!=null){<br />conn.close();<br /> }<br /> return realConn;<br />}<br />
  7. 7. 尽可能Nonblocking例子<br />改造想法<br />能否不创建无谓的连接,想到了FutureTask<br />private static ConcurrentHashMap<String,FutureTask<Connection>> connections=new ConcurrentHashMap<String,FutureTask<Connection>>();<br />public Connection get(final String key) throws Exception{<br /> if(connections.containsKey(key)){<br /> return connections.get(key).get;<br />}<br />FutureTask<Connection> newTask=new FutureTask<Connection>(new Callable<Connection>(){<br /> public Connection call() throws Exception{<br /> return createConnection(key);<br /> }<br />});<br />FutureTask<Connection> task=connections.putIfAbsent(key,newTask);<br />if(task==null){<br />newTask.run();<br /> return newTask.get();<br /> }<br /> return task.get();<br />}<br />
  8. 8. 线程<br />线程交互<br />wait/notify(notifyAll)<br />在测kilim一个版本时,高压力的情况下wait/notify貌似有bug,jdk是1.6.0_07<br />线程池<br />ThreadPoolExecutor做的已经不错了,但要注意合理使用<br />不要使用无限制大小的线程池<br />最好自行实现ThreadFactory,最少给线程加上个前缀<br />当超过coreSize后,会扔到指定的BlockingQueue中,因此要注意这个地方...<br />
  9. 9. 线程<br />线程跟踪和问题查找<br />jstack、tda、visualvm<br />jprofiler<br />pstat+jstack直接查找耗cpu的线程<br />
  10. 10. 线程调度<br />最常见的问题在于必须是一个请求占据一个线程,无论这个请求中途是否要去远程访问、文件IO或锁等待;<br />Coroutinemaybe是可以考虑的方案<br />轻量级线程<br />基于栈分析保存相关的上下文信息:会多消耗内存<br />要求整个处理过程中不能有阻塞<br />意味着锁、数据库访问、网络访问都得处理好<br />Java中:Scala、Kilim<br />
  11. 11. Some tips<br />多线程后一定要考虑对资源的消耗,否则搞不好性能反倒更差了<br />CPU<br />cpu切换是否太严重<br />内存<br />内存消耗是否严重,在网络通信中最明显<br />性能<br />尽可能Non-Blocking<br />拆分锁<br />隔离(ReadWrite)<br />CopyOnWrite,允许读脏数据<br />

×