Multi-Core in JVM/Java: Concurrent Programming in Java Prior Java 5 Java 5 (2006) Java 7 (2010) Other Topics
Multi-Core in JVM/Java: Concurrent Programming in Java Prior Java 5 Java 5 (2006) Java 7 (2010) Other Topics
Prior Java 5
Java 5 (2006)
Java 7 (2010)
Other topics
Threads
Synchronization
Java Threads
z
Java Threads
z
Synchronized Methods
z
Synchronized Statements
z
Finer-grained synchronization
Java 5 (2006)
z
java.util.concurrent
java.util.concurrent.atomic
java.util.concurrent.locks
Atomic Objects
z
Lock objects
z
Interfaces:
ReadWriteLock
Condition
Lock
Executor framework
z
Callable
Future
Executor
ExecutorService
ScheduledExecutorService
Executor
z
Examples:
class DirectExecutor implements Executor
{
public void execute(Runnable r) {
r.run();
}
}
class ThreadPerTaskExecutor implements Executor
{
public void execute(Runnable r) {
new Thread(r).start();
}
}
Executor
z
Example of ScheduledExecutorService
class BeeperControl {
private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
public void beepForAnHour() {
final Runnable beeper = new Runnable() {
public void run() { System.out.println("beep"); }
};
final ScheduledFuture<?> beeperHandle =
scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
scheduler.schedule(new Runnable() {
public void run() { beeperHandle.cancel(true); }
}, 60 * 60, SECONDS);
}
}
ThreadPoolExecutor
z
Queues
z
Classes: LinkedBlockingQueue,
ArrayBlockingQueue, SynchronousQueue,
PriorityBlockingQueue, DelayQueue
Synchronizers
z
Semaphore
CountDownLatch
CyclicBarrier
Exchanger
Concurrent Collections
z
ConcurrentHashMap
CopyOnWriteArrayList
CopyOnWriteArraySet
Concurrency in Java 7
z
z
Fork-join framework
ParallelArray
Fork-join framework
z
Fork-join framework
z
java.util.concurrent.forkjoin
Work Stealing
Example
class MaxSolver extends RecursiveAction {
private final MaxProblem problem;
int result;
protected void compute() {
if (problem.size < THRESHOLD)
result = problem.solveSequentially();
else {
int m = problem.size / 2;
MaxSolver left, right;
left = new MaxSolver(problem.subproblem(0, m));
right = new MaxSolver(problem.subproblem(m,problem.size));
forkJoin(left, right);
result = Math.max(left.result, right.result);
}
}
}
ForkJoinExecutor pool = new ForkJoinPool(nThreads);
MaxSolver solver = new MaxSolver(problem);
pool.invoke(solver);
Performance
Results of Running select-max on 500k-element Arrays on various systems
Threshold=
500k
50k
5k
500
50
Pentium-4 HT (2 threads)
1.0
1.07
1.02
0.82
0.2
Dual-Xeon HT (4 threads)
0.88
3.02
3.2
2.22
0.43
1.0
5.29
5.73
4.53
2.03
0.98
10.46
17.21
15.34
6.49
ParallelArray
z
Filtering
Mapping
Replacement
Aggregation
Application
ParallelArray Example
ParallelArray<Student> students = new ParallelArray<Student>(fjPool, data);
double bestGpa = students.withFilter(isSenior)
.withMapping(selectGpa)
.max();
public class Student {
String name;
int graduationYear;
double gpa;
}
static final Ops.Predicate<Student> isSenior = new Ops.Predicate<Student>() {
public boolean op(Student s) {
return s.graduationYear == Student.THIS_YEAR;
}
};
static final Ops.ObjectToDouble<Student> selectGpa = new Ops.ObjectToDouble<Student>()
{
public double op(Student student) {
return student.gpa;
}
};
1.00
2.11
9.99
39.34
340.25
0.30
2.31
5.28
24.67
180.28
0.35
1.02
3.63
20.94
160.21
1.20
1.62
5.53
35.11
190.41
Other topics
z
Transactional Memory
Terracotta
z
http://www.terracotta.org
Transparent to programmer
Converts multi-threaded
application to a multi-JVM
(clustered) application.
Stream Programming
z
z
http://www.pervasivedatarush.com/
Based on dataflow graph, computation nodes
interconnected by queues
http://sourceforge.net/projects/high-scale-lib
ConcurrentAutoTable
z
NonBlockingHashMap
z
NonBlockingSetInt
z
STM or HTM
Transactional memory
Pros
Cons/problems
Transactions compose
Live-lock
No deadlocks
No Priority inversion
z
http://developers.sun.com/learning/javaoneonline/2008/pdf/TS-6316.pdf?cid=925329