introduction+to+java+concurrency

25
JACCRA

Upload: frank-appiah

Post on 27-Jan-2015

110 views

Category:

Documents


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Introduction+To+Java+Concurrency

JACCRA

Page 2: Introduction+To+Java+Concurrency

Frank AppiahSoftware Developer

Genkey Corp.

Introduction to Java Concurrency

http://frankappiahnsiah.wordpress.com

Page 3: Introduction+To+Java+Concurrency

RoadMap Background Concept Demo

Page 4: Introduction+To+Java+Concurrency

What is Java Concurrency ? Concurrency means existing or in operation at the same time

or together. Java provides : 1. Lock 2. Thread and Thread Management 3. Atomic It is in the package java.util.concurrency

Page 5: Introduction+To+Java+Concurrency

RoadMap Background Concept Demo

Page 6: Introduction+To+Java+Concurrency

LOCKS

Package: java.util.concurrent.locks

Page 7: Introduction+To+Java+Concurrency

What are the locks in Java? Lock Lock implementations provide more extensive locking operations

than can be obtained using synchronized methods and statements. They allow more flexible structuring, may have quite different properties, and may support multiple associated Condition objects.

ReadWriteLock A ReadWriteLock maintains a pair of associated locks, one for

read-only operations and one for writing. The read lock may be held simultaneously by multiple reader threads, so long as there are no writers. The write lock is exclusive.

Page 8: Introduction+To+Java+Concurrency

Lock A lock is a tool for controlling access to a shared resource by

multiple threads. Commonly, a lock provides exclusive access to a shared resource: only one thread at a time can acquire the lock and all access to the shared resource requires that the lock be acquired first. However, some locks may allow concurrent access to a shared resource, such as the read lock of a ReadWriteLock.

Page 9: Introduction+To+Java+Concurrency

ReentrantLock A reentrant mutual exclusion Lock with the same basic

behavior and semantics as the implicit monitor lock accessed using synchronized methods and statements, but with extended capabilities.

Page 10: Introduction+To+Java+Concurrency

Demo ReentrantLock reentrantLock=new ReentrantLock();

public int getNextSequenceID() { return getNewValue(); }

private int getNewValue() { reentrantLock.lock(); value+=1; reentrantLock.unlock(); return value; }

Lock acquired

Lock unacquired

Page 11: Introduction+To+Java+Concurrency

Different Implementation(edu.emory.mathcs.backport)

To use this implementation of Java ConcurrencyJust append the package name below to all java.util.concurrent

package classes. edu.emory.mathcs.backport

Page 12: Introduction+To+Java+Concurrency

Creating a Lock Factory with both implementations Check to see if edu.emory.mathcs.backport implementation

is available

static { try {

Class.forName("edu.emory.mathcs.backport.java.util.concurrent.locks.ReentrantLock");backportConcurrentPresent = true;

} catch (ClassNotFoundException ex) {backportConcurrentPresent = false;

}}

Page 13: Introduction+To+Java+Concurrency

Creating a Lock Implementation

if (JdkVersion.getMajorJavaVersion() >= JdkVersion.JAVA_15) {

return new JdkConcurrentLock(timeoutSeconds);} else if (backportConcurrentPresent) {

return new BackportConcurrentLock(timeoutSeconds);

Page 14: Introduction+To+Java+Concurrency

ATOMICS Package: java.util.concurrent.atomic

Page 15: Introduction+To+Java+Concurrency

Uses It can be use as an atomic update flag (AtomicBoolean) use as a sequential atomic counter (AtomicInteger) use as an atomically incremented sequence

number(AtomicLong) use to hold an atomic reference to an object

(AtomicReference<V>,V is the reference object)

Page 16: Introduction+To+Java+Concurrency

Demo

private AtomicLong atomic; public AtomicTest(long intialValue) { atomic=new AtomicLong(intialValue); }

public Long increment() { return atomic.incrementAndGet(); }

Page 17: Introduction+To+Java+Concurrency

Concurrent Threads

Package: java.util.concurrent

Page 18: Introduction+To+Java+Concurrency

What to look at? ThreadFactory ThreadPoolExecutor Callable (special form of Runnable class)

Page 19: Introduction+To+Java+Concurrency

ThreadFactory Implement a custom thread factory

There is a single method called newThread that takes Runnable as its parameter

Purpose : To create a new thread for the ThreadPoolExecutor when a new work requires a thread.

public class CustomThreadFactory implements ThreadFactory

Page 20: Introduction+To+Java+Concurrency

How to create a custom ThreadFactory?

public class CustomThreadFactory extends CustomThreadCreator implements ThreadFactory{

public CustomThreadFactory { super(); }

public CustomThreadFactory(String threadNamePrefix) { super(threadNamePrefix); }

public Thread newThread(Runnable r) {

return createThread(r); }

}

Page 21: Introduction+To+Java+Concurrency

ThreadPoolExecutor It can be schedule or not For scheduled thread executor

use :ScheduledThreadPoolExecutor class. A custom thread pool executor may be preferable.

Page 22: Introduction+To+Java+Concurrency

Demo Lets go to the code Walking through

Page 23: Introduction+To+Java+Concurrency

Callable This just like Runnable but this returns a result of the

execution task. Call the submit method of ThreadPoolExecutor instance and

pass in the Callable interface implementation.

Page 24: Introduction+To+Java+Concurrency

Thread ManagementThreadPoolExecutor methods for management: getTaskCount() getCompletedTaskCount() purge() shutdown() shutdownNow()