IBM tutorials - illyfrancis/scribble GitHub Wiki
Tech library
Multicore and threads
http://www.ibm.com/developerworks/library/j-nothreads/
Tutorials
Java concurrency
http://www.ibm.com/developerworks/training/kp/j-kp-concurrency/
- 5 things didn't know about j.u.c part 1
TimeUnitCopyOnWriteArrayListBlockingQueueConcurrentMapSynchronousQueues
- 5 things... part 2
Semaphorefor throttlingCountDownLatchExecutorinstead of creatingThreads directlyExecutor exec = getAnExecutorFromSomeplace(); exec.execute(new Runnable() { ... });ExecutorServiceExecutorService executor = Executors.newFixedThreadPool(..)
CallableandFuture
ScheduledExecutorService- Timeout methods
- also look at
.locksand.atomicpackage,CyclicBarrier
- Java concurrency bug patterns for multicore systems
volatilekeyword only guarantees that a variable is visible; it does not guarantee atomicity.- simultaneous locking on updated fields (to fix, use
private finalto ensure the lock object remains unchanged andmutexis guaranteed)
- read through the example but the gist is the synchronized field is updated with a 'different' instance
java.util.concurrentlock leak
- i.e. Lock.lock() invocation without unlock() on the same instance causes a lock leak (see below).
- performance tuning synchronized blocks (instead, reduce the scope of synch block to be more effective
- multi-stage access (where it needs access to multiple resources of which are individually thread safe but in combination or when you use them together they are not. hence need synchronized them)
- symmetric lock deadlock (read it again and look for Concurrency in Practice for workaround)
Example - lock leak
private final Lock lock = new ReentrantLock();
public void lockLeak() {
lock.lock();
try {
accessSharedResource(...);
lock.unlock();
} catch (Exception e) {}
}
public void accessSharedResource() throws InterruptedException {...}
Instead put the unlock invocation in the finally block.
public void lockLeak() {
lock.lock();
try {
accessSharedResource(...);
} catch (Exception e) {}
finally {
lock.unlock();
}
}
Actor concurrency
http://www.ibm.com/developerworks/training/kp/j-kp-actorconcurrency/