Java 并发编程实战 学习分享.ppt
《Java 并发编程实战 学习分享.ppt》由会员分享,可在线阅读,更多相关《Java 并发编程实战 学习分享.ppt(41页珍藏版)》请在麦多课文档分享上搜索。
1、Java 并发编程实战 学习分享,王清培,目录(要点):1.基础知识1.1.synchronized、ReentrantLock(内置锁与显示锁)1.2.内存可见性、volatile、ThreadLocal1.3.同步组件 CountDownLatch、FutureTask、Semaphore、CyclicBarrier2.进阶知识2.1.Executor线程池2.2.BoundQueue有界队列2.3.饱和策略3.原理知识3.1.原子变量3.2.CAS、自旋锁3.3.ABA问题、解决(AtomicStampedReference)3.4.非阻塞的链表算法3.5.AQS4.交流4.1.Doub
2、leCheck(双重检查锁的问题),1.基础知识1.1.synchronized、ReentrantLock(内置锁与显示锁)1.2.内存可见性、volatile、ThreadLocal1.3.同步组件 CountDownLatch、FutureTask、Semaphore、CyclicBarrier,开始!,1.1.synchronized、 ReentrantLock (内置锁与显示锁),1.synchronized(内置锁),JVM实现 偏向锁、轻量级锁需要手动打开,偏向锁 jdk1.6,轻量锁 jdk1.6,重量锁 原生,object.markword 保存锁的信息,lockrecor
3、d 线程加锁信息,锁升级: 偏向锁(只有一个thread lock)-轻量级锁(CAS lockrecored-markword)-重量级锁,单个Condition queue 原生,1.1.synchronized、 ReentrantLock (内置锁与显示锁),1.synchronized(内置锁) demo1 锁的方式:,锁定当前对象中的方法-锁定的是当前对象实例,锁定静态方法-锁定的是当前class对象,public synchronized void method() System.out.println(Thread.currentThread().getId(); ,publi
4、c synchronized static void statisMethod() System.out.println(Thread.currentThread().getId(); ,private Object lock = new Object(); public void method1() synchronized (lock) ,锁定代码块-锁定的是lock对象,1.1.synchronized、 ReentrantLock (内置锁与显示锁),1.synchronized(内置锁) demo2 简单的Condition queue:,public class Condition
5、Queue implements DemoRun private boolean isOk = false; Override public void runTest() throws ExecutionException, InterruptedException, CustomException, BrokenBarrierException Thread thread1 = new Thread() - synchronized (this) while (!isOk) try System.out.println(String.format(“t1:%s wait“, Thread.c
6、urrentThread().getId(); this.wait(); catch (InterruptedException e) e.printStackTrace(); System.out.println(String.format(“t1:%s 唤醒“, Thread.currentThread().getId(); ); thread1.start(); Thread thread2 = new Thread() - synchronized (this) while (!isOk) try System.out.println(String.format(“t2:%s wait
7、“, Thread.currentThread().getId(); this.wait(); catch (InterruptedException e) e.printStackTrace(); System.out.println(String.format(“t2:%s 唤醒“, Thread.currentThread().getId(); ); thread2.start(); Thread.sleep(2000); synchronized (this) isOk = true; this.notifyAll(); ,t1:12 wait t2:13 wait t2:13 唤醒
8、t1:12 唤醒,1.1.synchronized、Lock(内置锁与显示锁),2.ReentranLock(显示锁),基于AQS实现,相应Interruption,trylock,公平锁、非公平锁,多个Condition queue,1.1.synchronized、Lock(内置锁与显示锁),2.ReentranLock(显示锁) demo1 trylock 带有超时lock:,/* * nonFair非公平锁、fair公平锁 */ private ReentrantLock lock = new ReentrantLock(true); private void tryLockTest(
9、) throws InterruptedException Thread thread1 = new Thread() - lock.lock(); System.out.println(“thread1:“ + Thread.currentThread().getId(); try Thread.sleep(20000); catch (InterruptedException e) e.printStackTrace(); ); thread1.start(); Thread thread2 = new Thread() - try if (lock.tryLock(5, TimeUnit
10、.SECONDS) System.out.println(“thread2:“ + Thread.currentThread().getId(); try catch (Exception ignored) finally lock.unlock(); catch (InterruptedException e) e.printStackTrace(); ); thread2.start(); System.out.println(“main:“ + Thread.currentThread().getId(); Thread.sleep(10000); ,thread1:12 main:1,
11、1.1.synchronized、Lock(内置锁与显示锁),2.ReentranLock(显示锁) demo2 响应Interruption,thread2:java.lang.InterruptedException,/* * nonFair非公平锁、fair公平锁 */ private ReentrantLock lock = new ReentrantLock(true); private void lockInterrupted() Thread thread1 = new Thread() - lock.lock(); try Thread.sleep(10000); catch
12、(InterruptedException e) e.printStackTrace(); ); thread1.start(); Thread thread2 = new Thread() - try lock.lockInterruptibly(); lock.lock(); catch (InterruptedException e) System.out.println(String.format(“thread2:%s“, e); ); thread2.start(); try Thread.sleep(3000); catch (InterruptedException e) e.
13、printStackTrace(); thread2.interrupt(); ,1.1.synchronized、Lock(内置锁与显示锁),2.ReentranLock(显示锁) demo3 多个ConditionQueue 条件队列,public class ConditionDemo implements DemoRun private Lock lock = new ReentrantLock(); private Condition condition1 = lock.newCondition(); private Condition condition2 = lock.newCo
14、ndition(); private class CustomThread extends Thread public CustomThread(String threadName, Runnable runnable) super(runnable); this.setName(threadName); Override public void runTest() throws ExecutionException, InterruptedException, CustomException, BrokenBarrierException for (int i = 0; i try lock
15、.lock(); condition1.await(); System.out.println(String.format(“t:%s 唤醒“, Thread.currentThread().getName(); catch (InterruptedException e) e.printStackTrace(); finally lock.unlock(); ); thread1.start(); CustomThread thread2 = new CustomThread(String.valueOf(i), () - try lock.lock(); condition2.await(
16、); System.out.println(String.format(“t2:%s 唤醒“, Thread.currentThread().getName(); catch (InterruptedException e) e.printStackTrace(); finally lock.unlock(); ); thread2.start(); Thread.sleep(3000); lock.lock(); condition1.signalAll(); lock.unlock(); Thread.sleep(3000); lock.lock(); condition2.signalA
17、ll(); lock.unlock(); ,t:0 唤醒 t:1 唤醒 t:2 唤醒 t:3 唤醒 t:4 唤醒 t:5 唤醒 t:6 唤醒 t:7 唤醒 t:8 唤醒 t:9 唤醒 t2:0 唤醒 t2:1 唤醒 t2:2 唤醒 t2:3 唤醒 t2:4 唤醒 t2:5 唤醒 t2:6 唤醒 t2:7 唤醒 t2:8 唤醒 t2:9 唤醒,可以方便实现读写锁特性,1.2.内存可见性、volatile、ThreadLocal,4.优先使用Volatile 解决可见性、重排序问题,5.ThreadLocal 封闭进当前线程,3.JMM:Happens-before 原则,1.多核处理器带来的内存
18、可见性问题,2.指令重排序问题,1.程序顺序规则,2.监视器锁规则,3.Volatile变量规则,4.线程启动规则,5.程序结束规则,6.中断规则,7.终结器规则,8.传递性规则,1.2.内存可见性、volatile、ThreadLocal,1. ThreadLocal demo:,public class ThreadLocalDemo implements DemoRun private ThreadLocal threadLocal = new ThreadLocal(); Override public void runTest() for (int i = 0; i Product
19、product = new Product(Thread.currentThread().getId(); this.threadLocal.set(product); System.out.println(“set.threadId:“ + product.getThreadId() + “.“ + Thread.currentThread().getId(); product = this.threadLocal.get(); System.out.println(“get.threadId:“ + product.getThreadId() + “.“ + Thread.currentT
20、hread().getId(); ).start(); ,1.3.同步组件 CountDownLatch、FutureTask、Semaphore、CyclicBarrier,1.CountDownLatch demo1 闭锁:,public class CountDownLatchDemo implements DemoRun private CountDownLatch countDownLatch = new CountDownLatch(3); public void runTest() throws InterruptedException for (int i = 0; i try
21、 System.out.println(Thread.currentThread().getId() + “.begin run.“); Thread.sleep(1000); System.out.println(Thread.currentThread().getId() + “.end run.“); countDownLatch.countDown(); catch (InterruptedException e) e.printStackTrace(); ); thread.start(); countDownLatch.await(); System.out.println(Thr
22、ead.currentThread().getId() + “master thread run.“); System.out.println(Thread.currentThread().getId() + “master thread end.“); ,12.begin run. 13.begin run. 14.begin run. 14.end run. 13.end run. 12.end run. 1master thread run. 1master thread end.,1.3.同步组件 CountDownLatch、FutureTask、Semaphore、CyclicBa
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- JAVA 并发 编程 实战 学习 分享 PPT
