Speakers: Alex Vazquez and Guillermo Muñoz
Topics:
- Introduction to Java Concurrency Framework (JCF)
- Analysis of issues without using JCF
- Review of concurrent collections in JCF
- Review of Lock implementation
do it. • Thread instances + Runnable implementations class MyTask implements Runnable { @Override public void run() { // do whatever is needed } } Thread t = new Thread(new MyTask()); t.start(); Thread t = new Thread(new Runnable() { @Override public void run() { // do whatever is needed } }); t.start(); 3
execution is accessing the same resources (variables, files, network connections, etc) at the same time, disregarding other threads that may use the same resources. public int foo; while(crazyCondition()) { weirdStuff(); if(++foo > MAX_VALUE) { foo = 0; } } if(someOtherCrazyCondition()) { if(--foo < 0) { foo = MAX_VALUE; } } 4
cache. • Readers-Writers • Simultaneous access to read-only processes • Exclusive access for write processes • Producers-Consumers • Coordinated production and consumption of resources. 6
thread holding a lock until another thread tells it “something” happened. • notify() schedules a waiting thread to be awaken due to “that something” happening. 7
• Improved scalability: less locks = more concurrent threads • Weakly-consistent iterators: snapshots = no more ConcurrentModificationException • Built-in composite ops: no unexpected race conditions. Problem Consequence Workaround One lock per collection Long waits None Fail-fast iterators ConcurrentModificationException Create a local copy No composite-ops Race conditions Use synchronized blocks 17
over underlying array (add, set, remove, etc) using ReentrantLock • Be careful: literally copy all the array • Snapshot style iterator: no changes to original array (read-only) • Use when: • Reads >> writes • Small number of elements • Better performance than: Vector, Collections.synchronized[List|Set]() 18
until to non-empty queue, put() blocks until space to store. Blocking queue Capacity Backed by SynchronousQueue Bounded (1) E ArrayBlockingQueue Bounded E[] LinkedBlockingQueue Bounded | Unbounded Node<E> linked list PriorityBlockingQueue Unbounded PriorityQueue<E> DelayQueue Unbounded PriorityQueue<Delayed> 20
abstraction • Specializes in process synchronization and coordination • Provides enhanced semantics • Introduces the Conditions, which enable to signal separate thread notification when specific conditions are being waited for. 21