SlideShare a Scribd company logo
Practical Concurrency January, 2009 http://javadom.blogspot.com/2011/06/lecture-on-java-concurrency-day-1.html
Contents Concurrency Basics Designing Concurrency Concurrent Utils
Day 1 (9am, Jan 21, 2009) Hardware Basics Thread Basics Thread Revisited
1-1 Hardware Basics Processor Interconnect Memory Cache
Hardware
1-2 Thread Basics Race Condition Lock and Condition interrupt
Race condition Competing condition against some  shared resources Example T1 : var += 5 T2 : var *= 5 Initially var was 0, then what’s the result value? OS scheduling Critical Section a piece of code that accesses a shared resource that must not be concurrently accessed by more than one thread of execution
Lock and Condition Monitor object Synchronized block Wait and notify on monitor object Spurious wakeup Lock and Condition java.util.concurrent.locks package Lock, Condition, ReadWriteLock interfaces ReentrantLock, ReentrantReadWriteLock classes
interrupt static boolean Thread.interrupted Tells current interrupt flag and reset Boolean Thread.isInterrupted Just tell current interrupt flag InterruptedException Object.wait Thread.sleep
1-3 Thread Revisited Spurious wakeup Notify or notifyAll Volatile and visibility
Spurious Wakeup http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html#wait() A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup. While this will rarely occur in practice, applications must guard against it by testing for the condition that should have caused the thread to be awakened, and continuing to wait if the condition is not satisfied. Always use guarded wait Wait should be used in guarded loops
Notify or NotifyAll Notify Wake up only one thread if any was waiting less overhead (better performance) Applicable on limited cases NotifyAll All the waiting threads can check its guard condition
Java Memory Model Locking Atomicity Visibility Volatile Visibility only Ordering Intra-thread semantics only Synchronization order is total ordering among threads which locks or unlocks the lock

More Related Content

KEY
Advanced Java I
PPT
Chapter 15
PPTX
Macro teradata
PPTX
Java Exception Handling
PPTX
Exception handling in java
ODP
Exception Handling In Java 15734
PPTX
Exception handling in java
Advanced Java I
Chapter 15
Macro teradata
Java Exception Handling
Exception handling in java
Exception Handling In Java 15734
Exception handling in java

What's hot (20)

PPT
exception handling in java
PDF
Effective java 摘選條目分享 3 - concurrency
PDF
Java Exception handling
PPTX
Exception handling in JAVA
PPTX
Exceptions in Java
PPTX
Java class 7
ODP
Double checkedlockingjavasingletons
PDF
Section 7 fundamentals
PPTX
Exception handling in java
PPTX
Exception handling in java
PPTX
Exception handling in java
PDF
summarizer16fev16_Exceptions
PPT
Exception handling
PDF
Java exception handling
PDF
Java Pitfalls and Good-to-Knows
PPTX
Presentation on-exception-handling
PPTX
Java Exception Handling and Applets
PDF
Java multi threading and synchronization
PDF
B.Sc. III(VI Sem) Advance Java Unit1: Exception Handling & Multithreading
PPT
exception handling
exception handling in java
Effective java 摘選條目分享 3 - concurrency
Java Exception handling
Exception handling in JAVA
Exceptions in Java
Java class 7
Double checkedlockingjavasingletons
Section 7 fundamentals
Exception handling in java
Exception handling in java
Exception handling in java
summarizer16fev16_Exceptions
Exception handling
Java exception handling
Java Pitfalls and Good-to-Knows
Presentation on-exception-handling
Java Exception Handling and Applets
Java multi threading and synchronization
B.Sc. III(VI Sem) Advance Java Unit1: Exception Handling & Multithreading
exception handling
Ad

Similar to Lecture on Java Concurrency Day 1 on Jan 21, 2009. (20)

PPT
Reliable and Concurrent Software - Java language
PPTX
Concurrency with java
PPTX
Concurrency with java
PPTX
Concurrency with java
PPTX
Concurrency with java
PPTX
Concurrency with java
PPTX
Concurrency with java
PPTX
Concurrency with java
PPTX
The Java Memory Model
PDF
Concurrency
PPT
Programming - Java-Threads-and-Synchronization.ppt
PPT
Java Multithreading and Concurrency
PPTX
Java Concurrency and Asynchronous
PDF
Java concurrency in practice
PPT
04 threads
PPTX
Николай Папирный Тема: "Java memory model для простых смертных"
PDF
Study effective java item 78 synchronize access to mutable data
PDF
Monitors and Blocking Synchronization : The Art of Multiprocessor Programming...
KEY
Modern Java Concurrency (OSCON 2012)
PPT
BIL406-Chapter-9-Synchronization and Communication in MIMD Systems.ppt
Reliable and Concurrent Software - Java language
Concurrency with java
Concurrency with java
Concurrency with java
Concurrency with java
Concurrency with java
Concurrency with java
Concurrency with java
The Java Memory Model
Concurrency
Programming - Java-Threads-and-Synchronization.ppt
Java Multithreading and Concurrency
Java Concurrency and Asynchronous
Java concurrency in practice
04 threads
Николай Папирный Тема: "Java memory model для простых смертных"
Study effective java item 78 synchronize access to mutable data
Monitors and Blocking Synchronization : The Art of Multiprocessor Programming...
Modern Java Concurrency (OSCON 2012)
BIL406-Chapter-9-Synchronization and Communication in MIMD Systems.ppt
Ad

More from Kyung Koo Yoon (13)

PDF
Kubernetes
PDF
Java 8 고급 (6/6)
PDF
Java 8 고급 (5/6)
PDF
Java 8 고급 (4/6)
PDF
Java 8 고급 (3/6)
PDF
Java 8 고급 (2/6)
PDF
Java 8 고급 (1/6)
PDF
Spring Framework - Inversion of Control Container
PDF
Smart software engineer
PPT
Lecture on Java Concurrency Day 3 on Feb 11, 2009.
PPT
Lecture on Java Concurrency Day 2 on Feb 4, 2009.
PPT
Lecture on Java Concurrency Day 4 on Feb 18, 2009.
PPTX
창의와 열정, 소프트웨어 엔지니어
Kubernetes
Java 8 고급 (6/6)
Java 8 고급 (5/6)
Java 8 고급 (4/6)
Java 8 고급 (3/6)
Java 8 고급 (2/6)
Java 8 고급 (1/6)
Spring Framework - Inversion of Control Container
Smart software engineer
Lecture on Java Concurrency Day 3 on Feb 11, 2009.
Lecture on Java Concurrency Day 2 on Feb 4, 2009.
Lecture on Java Concurrency Day 4 on Feb 18, 2009.
창의와 열정, 소프트웨어 엔지니어

Recently uploaded (20)

PDF
cuic standard and advanced reporting.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Advanced Soft Computing BINUS July 2025.pdf
PDF
Approach and Philosophy of On baking technology
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
Empathic Computing: Creating Shared Understanding
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Advanced IT Governance
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
PDF
Modernizing your data center with Dell and AMD
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Spectral efficient network and resource selection model in 5G networks
PPT
Teaching material agriculture food technology
cuic standard and advanced reporting.pdf
Understanding_Digital_Forensics_Presentation.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Machine learning based COVID-19 study performance prediction
Advanced Soft Computing BINUS July 2025.pdf
Approach and Philosophy of On baking technology
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
GamePlan Trading System Review: Professional Trader's Honest Take
Empathic Computing: Creating Shared Understanding
Dropbox Q2 2025 Financial Results & Investor Presentation
Advanced IT Governance
Review of recent advances in non-invasive hemoglobin estimation
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
Modernizing your data center with Dell and AMD
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Spectral efficient network and resource selection model in 5G networks
Teaching material agriculture food technology

Lecture on Java Concurrency Day 1 on Jan 21, 2009.

  • 1. Practical Concurrency January, 2009 http://javadom.blogspot.com/2011/06/lecture-on-java-concurrency-day-1.html
  • 2. Contents Concurrency Basics Designing Concurrency Concurrent Utils
  • 3. Day 1 (9am, Jan 21, 2009) Hardware Basics Thread Basics Thread Revisited
  • 4. 1-1 Hardware Basics Processor Interconnect Memory Cache
  • 6. 1-2 Thread Basics Race Condition Lock and Condition interrupt
  • 7. Race condition Competing condition against some shared resources Example T1 : var += 5 T2 : var *= 5 Initially var was 0, then what’s the result value? OS scheduling Critical Section a piece of code that accesses a shared resource that must not be concurrently accessed by more than one thread of execution
  • 8. Lock and Condition Monitor object Synchronized block Wait and notify on monitor object Spurious wakeup Lock and Condition java.util.concurrent.locks package Lock, Condition, ReadWriteLock interfaces ReentrantLock, ReentrantReadWriteLock classes
  • 9. interrupt static boolean Thread.interrupted Tells current interrupt flag and reset Boolean Thread.isInterrupted Just tell current interrupt flag InterruptedException Object.wait Thread.sleep
  • 10. 1-3 Thread Revisited Spurious wakeup Notify or notifyAll Volatile and visibility
  • 11. Spurious Wakeup http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html#wait() A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup. While this will rarely occur in practice, applications must guard against it by testing for the condition that should have caused the thread to be awakened, and continuing to wait if the condition is not satisfied. Always use guarded wait Wait should be used in guarded loops
  • 12. Notify or NotifyAll Notify Wake up only one thread if any was waiting less overhead (better performance) Applicable on limited cases NotifyAll All the waiting threads can check its guard condition
  • 13. Java Memory Model Locking Atomicity Visibility Volatile Visibility only Ordering Intra-thread semantics only Synchronization order is total ordering among threads which locks or unlocks the lock

Editor's Notes

  • #9: Quiz : 두 개 ( 혹은 여러 개 ) 의 쓰레드가 동시에 다음 코드를 실행할 때 value 가 가질 수 있는 가능한 값은 ? (value 의 초기값은 0) public void run() { while (true) { value = value + 5; value = value - 5; } }
  • #10: Lock 인터페이스는 자바의 동기화 블럭 표현 방식인 synchronized 블럭 대신에 명시적인 잠금 획득과 잠금 해제를 사용하는 방식이다 . C/C++ 에서 제공하는 잠금 방식과 유사한 방식으로 코드 차원에서 잠금을 반드시 풀어줘야 한다는 제약이 있지만 블럭 구조가 아닌 방식 , 즉 필요에 따라 잠금 획득과 잠금 해제가 완전히 다른 메쏘드나 블럭에서 처리될 수도 있으며 , 잠금을 획득할 때 tryLock() 을 사용할 수 있다는 장점이 있다 . Condition 인터페이스는 자바의 동기화 이벤트 표현 방식인 wait(), notify() 메쏘드에 대응하는 개념으로 wait(), notify() 메쏘드들이 대응하는 synchronized 블럭 안에서 동작하듯이 Condition 객체는 대응하는 Lock 객체를 통해 동작한다 . Condition 객체를 사용할 때의 장점으로는 하나의 Lock 객체에서 여러 개의 Condition 객체를 사용할 수 있다는 점을 들 수 있다 . class BoundedBuffer { final Lock lock = new ReentrantLock(); final Condition notFull = lock.newCondition(); final Condition notEmpty = lock.newCondition(); final Object[] items = new Object[100]; int putptr, takeptr, count; public void put(Object x) throws InterruptedException { lock.lock(); try { while (count == items.length) notFull.await(); items[putptr] = x; if (++putptr == items.length) putptr = 0; ++count; notEmpty.signal(); } finally { lock.unlock(); } } public Object take() throws InterruptedException { lock.lock(); try { while (count == 0) notEmpty.await(); Object x = items[takeptr]; if (++takeptr == items.length) takeptr = 0; --count; notFull.signal(); return x; } finally { lock.unlock(); } } }
  • #13: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html#wait() A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup. While this will rarely occur in practice, applications must guard against it by testing for the condition that should have caused the thread to be awakened, and continuing to wait if the condition is not satisfied. In other words, waits should always occur in loops, like this one: synchronized (obj) { while () obj.wait(timeout); ... // Perform action appropriate to condition }
  • #14: Possible Notify Cases All possible waiting threads are necessarily waiting for conditions relying on the same notifications, usually the exact same condition Each notification intrinsically enables at most a single thread to continue. Thus it would be useless to wake up others. Can accommodate uncertainties surrounding the possibility that an interrupt and a notify may occur at about the same time. In this case, the one thread that was notified is about to terminate. You might want another thread to receive notification instead, but this is not automatically arranged. (The issue does not arise with notifyAll since all threads are woken)