© 2010 IBM Corporation Concurrency Demystified Ajith Ramanath | Senior Developer | JTC-ISL
© 2010 IBM Corporation 2 What’s in store today? Thinking Performance & Scalability Erstwhile Java Synchronization Tools & Diagnostics Java Memory Model Concurrency & Non-Blocking Synchronization Concurrent Building Blocks in Java A Simple Concurrent Data-structure Overview of Project Amino References
© 2010 IBM Corporation 3 Thinking Performance & Scalability… What are multi-core systems? Scaling UP (⇑) vs. Scaling OUT (⇔) What are scalable programs? Relevance of scalable software
© 2010 IBM Corporation 4 Erstwhile Java Synchronization Java “synchronized” keyword Object specific “mutex” Applies to class and instance Objects 2 or 3 Tier lock strategy Java semaphore implementation Public wait() and notify() / notifyAll() methods Accessible from all Java classes and objects JSE specification provides semaphore via ‘ java.lang.Object ’ class Lock contention & ensuing problems Reduced parallelism & lower CPU utilization Increased thread context switch time
© 2010 IBM Corporation 5 JLM Report Tools: Java Lock Monitor (JLM) JVMPI/TI agent for profiling Java monitor entry and exit Free Download from SourceForgeSourceForge Available on Linux and Windows (x86_64 && IA32) Simple to use and Light-weight Java Virtual Machine (JVM)JPROF rtdriver Socket JVMPI Enable / Disable Events Request Events JVM callback Textual commands Reports / Errors
© 2010 IBM Corporation 6 Sample JLM Output
© 2010 IBM Corporation 7 Timer based profiler – works using sampling technique Tracks CPU ticks and maps them to kernel / application modules Useful to investigate CPU over/under-utilization Pick tprof ‘first’ during performance analysis! Tools: tprof JIT module taking the maximum clock ticks
© 2010 IBM Corporation 8 Java Memory Model Java goofed up…before v5.0: The ugly DCL paradigm ( A popular anti-pattern in Java! ) (Im?)mutability Compiler re-ordering / volatiles & non-volatile stores Immutability ProblemProblem with volatile/non-volatile storesDouble-checked Locking
© 2010 IBM Corporation 9 Concurrent Computing (to the rescue…) Definitions: Concurrent computing is a form of computing in which programs are designed as collections of interacting computational processes that may be executed in parallel A non-blocking algorithm ensures that threads competing for a shared resource do not have their execution indefinitely postponed by mutual exclusion.
© 2010 IBM Corporation10 Concurrent Building Blocks (CBBs) in Java Atomic variables Reentrant locks Collection APIs Semaphores Latches Cyclic Barriers Future Semantics Executors Exchangers Since Java 50 (TIGER) release
© 2010 IBM Corporation11 Java Collection CBBs (A closer look…) Queue Implementations: BlockingQueue Deque ConcurrentLinkedQueue Map Implementations: ConcurrentHashMap ConcurrentSkipListMap (since Java 60) Set Implementations: CopyOnWriteArraySet ConcurrentSkipListSet (since Java 60) List Implementations: CopyOnWriteArrayList
© 2010 IBM Corporation12 A Simple Concurrent API (Treiber’s Algorithm) Class ConcurrentStack { static AtomicReference top = new AtomicReference (); public void push (Object item) { Node newHead = new Node(item); do { Node oldHead = top.get(); newHead.next = oldHead; } while (!top.compareAndSet(oldHead, newHead); } public Object pop() { Node newHead, oldHead; do { oldHead = top.get(); if (oldHead == null) return null; newHead = oldHead.next(); } while (!top.compareAndSet(oldHead, newHead); return oldHead.item; }
© 2010 IBM Corporation13 Project Amino Building a scalable foundation for multicore programming Multiplatform - Java and C++ Programming tools and libraries Collaborative work between IBM research and IBM Software Labs Available for download now on SouceForge Open for your participation and contribution!
© 2010 IBM Corporation14 Amino Data Structures Amino data- structures are designed to have lock-free or wait-free semantics A lock-free operation allows optimistic concurrency, enables isolated thread failure and will not deadlock A wait-free operation will always proceed without blocking Focus on Java Data structures complement Java’s JUC Stack Vector HashMap QueueDeque Dictionary Tree Priority Queue List Binary srch tree
© 2010 IBM Corporation15 References & Further Reading Java Concurrency in Practice (by Brian Geotz – Pearson Education) Oracle Java APIs Download siteOracle Java APIs Download site High Performance Dynamic Lock-Free Hash Tables and List-Based SetsHigh Performance Dynamic Lock-Free Hash Tables and List-Based Sets Scalable and Lock-Free Concurrent DictionariesScalable and Lock-Free Concurrent Dictionaries Lock-free Dynamically Resizable ArraysLock-free Dynamically Resizable Arrays Lock-free Resizable Hash TablesLock-free Resizable Hash Tables Fast and Lock-Free Concurrent Priority Queues for Multi-Thread SystemsFast and Lock-Free Concurrent Priority Queues for Multi-Thread Systems IBM DeveloperWorks series: Java Theory and Practice: Fixing the Java Memory Model Part 1 and 2Part 12 Finally, use Wikipedia and Google Search Engine for more!
© 2010 IBM Corporation16 I’m done…are there (more?) questions?