1 / 23

Java Concurrency Programming

Java Concurrency Programming. joez@hz.webex.com. Agenda. Multi-Thread Introduction Java Memory Mode Synchronized Concurrent Package Introduction Deadlock Thread Pool Multi-thread Anti-pattern Concurrent Programming Best Practice Q&A. Multi-Thread introduction -related conception.

teige
Download Presentation

Java Concurrency Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Concurrency Programming joez@hz.webex.com

  2. Agenda Multi-Thread Introduction JavaMemory Mode Synchronized Concurrent Package Introduction Deadlock Thread Pool Multi-thread Anti-pattern Concurrent Programming Best Practice Q&A

  3. Multi-Thread introduction-related conception • What is thread? • Concurrent VS Parallel • Performance VS Multi-Thread

  4. Multi-Thread introduction -Thread Life Circle

  5. Multi-Thread introduction -Thread Safe* • Conception 当多个线程访问一个类时,如果不用考虑这些线程在运行环境下的调度和交替执行,并且不需要额外的同步而且调用方法代码时不需要进行其他协调,这个类的行为依然是正确的,那么称这个类是线程安全的 • Atomic operation: • 在多进程(线程)的操作系统中不能被其它进(线程)打断的操作就叫原子操作 • Visibility • volatile的语义, 其实是告诉处理器, 不要将我放入工作内存, 请直接在主存操作我 因此, 当多核或多线程在访问该变量时, 都将直接 操作 主存, 这从本质上, 做到了变量共享 • 锁 • 多线程中用来保证操作原子性,变量可见性的最有效手段,但也是程序性能,可伸缩性的主要杀手,原则是能用原子变量或volatile替换就用他们替换,能不用锁尽量别用,能用共享锁就别用独占锁,读操作大大多于写操作尽量用读写锁,能采用锁分离策略尽量采用锁分离策略

  6. Multi-Thread introduction -Thread Safe* ThreadLocalObj1 ThreadLocalObj1 Value Value Key Value ThreadLocalObj2 ThreadLocalObj2 Value Value Thread1 • 线程安全的常见手段 • 让对象无状态 • ThreadLocal 隔离 • 不可变对象天生就是线程安全的 • 让对象只在栈范围内可见 • 用锁保护对象状态 ThreadLocalMap ThreadLocalObj3 ThreadLocalObj3 Value Value ThreadLocalMap Thread2 ThreadLocalMap Thread3

  7. CPU core1 core2 OS task scheduler thread1 thread2 thread3 thread4 Stack Stack Stack Stack Heap Perm gen Heap Native heap obj obj obj code JavaMemory Mode CPU CPU CPU core1 core2 core1 core2 core1 core2

  8. Synchronized-example1 public static synchronized void method1 (){ //block } public synchronized void method4 (){ //block } public synchronized void method5 (){ //block } • public void method2 (){ • synchronized (this.class){ • //block • } • } • public void method6 (){ • synchronized (this){ • //block • } • }

  9. Synchronized-example2 public void method1 (){ while(status == 1){ wait(); } //do something } public void method2 (){ //do something notifyAll(); }

  10. Synchronized-notify, notifyAll, wait* Notify/NotiffyAll

  11. Concurrent Package Introduction-Atomic Type* • 原子类型: • AtomicBoolean, AtomicInteger, AtomicIntegerArray, AtomicLong, AtomicReference • 原子类型特点: • 原子类型没有使用锁, 是无阻塞,通过使用volatitile和CPU原子原子语义CAS实现原子操作

  12. Concurrent Package Introduction -Synchronizer* • BlockQueue • ConcurrentLinkedQueue • Semaphore • CyclicBarrier • CountDownLatch • FutureTask • Lock • Synchronizer特征: • 他们封闭状态,这些状态决定线程执行到某一点时是通过还是被迫等待。

  13. Concurrent Package Introduction -Executor Framework* • Executor • ExecutorService • CompletionService

  14. Concurrent Package Introduction -Lock ReentrantLock 可重入,可中断,可非阻塞,支持公平非公平策略,支持多个条件等待队列, Synchronized最佳替代品 ReentrantReadWriteLock 具有ReentryLock所有优点,并且允许多线程并发读,Synchronized无法实现同样功能 应用场景:对于那些读多写少的共享对象状态保护

  15. Concurrent Package Introduction -Lock in DMS(1)* ItemState ReadWriteLock Session1-Thread Read IS Write Session2-Thread IS IS Read Session3-Thread IS

  16. Concurrent Package Introduction -Lock in DMS(2)* Get Lock and check conflict wait DB store notify A,B,C Thread1 B,C Thread2 A,N Thread3 CS Thread4 Thread5 DF

  17. Deadlock

  18. Deadlock(2) Lock writeLock = rwLock.writeLock(); writeLock.lock(); capacityLock.lock(); try { while (capacity > 100) { empty.await(); } capacity++; full.signalAll(); } finally { capacityLock.unlock(); writeLock.unlock(); }

  19. Thread Pool • 线程池的使用场景 • 工具类ThreadPoolExecutor • 线程池的风险: • 死锁、资源不足、并发错误、线程泄漏、请求过载 • 线程使用准则 • 不要对那些同步等待其它任务结果的任务排队。 • 在为时间可能很长的操作使用合用的线程时要小心。 • 理解任务。要有效地调整线程池大小,您需要理解正在排队的任务以及它们正在做什么。

  20. thread Servlet Container create thread thread Thread pool thread Instantiate Servlet Servlet instance Call init() method initialize instance Call service method Assign a thread to request process request Client1 Call service method Assign a thread to request process request Client2 Thread Pool-Servlet Container Struts2 MVC Layer Business Layer persistent Layer action action action Singleton Manager action Singleton Manager

  21. Multi-thread Anti-pattern • 在构造函数中启动线程 • 不完全的同步 • 在使用某个对象当锁时,改变了对象的引用,导致同步失效 • 没有在循环中调用wait() • 同步的范围过小或者过大 • 不正确使用volatile

  22. Concurrent Programming Best Practice • 访问共享数据前问问自己是否需要进行同步 • 尽量避免一次操作拿多把锁(容易产生死锁) • 尽量避免在同步块内调用其他对象的方法 • 尽可能减少锁持有时间 • 使用显示锁时一定要遵循try..finally模式 • 尽可能使用现有的1.5同步工具类进行同步 • 使用volatile只能增加可见性,没有同步语义 • 不要改变默认线程优先级 • 尽量减少共享对象的状态 • 谨慎对待InterruptException,不要轻易忽略

  23. Q&A

More Related