3. Multiple Threads with in the same program can be
scheduled simultaneously on multiple CPUs.
Most modern operating systems treat threads, not
processes, as the basic units of scheduling
~Java concurrency in practice
4. On a computer with multiprocessors, processes or
threads can run on different processors
~MSDN Threads and Processes
7. Threads are everywhere
● JVM creates thread for GC
● AWT, Swing and JavaFX use event dispatch
thread
● Timer for deferred tasks
● Application server handles multiple client
– Servlet must be thread-safe
● RMI
8. What is Thread?
● Process
– A program in execution
– Providing the resources needed to execute a
program
– One process can't access or modify other
process
9. What is Thread?
● Thread
– A basic unit of CPU utilization
– Lightweight process (LWP)
– Multiple threads are executed within a
process
● Share process's virtual address space
and system resource
12. Multithreading Models
● User threads
– Efficient, flexible
– Above the kernel, without kernel support
● Kernel threads
– kernel can assign one thread to each logical core
in a system
14. Java on each OS
● Windows
– Native thread(Windows 95/NT)
● Linux
– Native thread since JDK 1.3
– LinuxThread, NPTL(Since Red Hat 9)
● FreeBSD
– Native thread since JDK 1.3.1
– libpthread(FreeBSD 5.3)
– libthr(FreeBSD 7)
18. So, Why Thread in Java?
● Thread is inescapable feature of Java
● Take advantage of multiprocessor system
● Simplify modeling
● Thread is cheap
● Don't need to worry about memory
model in different environment
20. Thread safety
● Behaves correctly when accessed from multiple
threads, and there is no synchronization or
coordination on caller
– java.text.SimpleDateFormat is not thread safe
– Stateless servlet is safe
21. Race conditions
● The output is dependent on the sequence or
timing of other uncontrollable events
1)
if(!map.containsKey(key)){
map.put(key,value);
}
2)
int n;
int calculate(){
return n++;
}
22. Synchronized
● Only one thread can execute the block of code
protected by the same lock at the same time
● Ensures that each thread entering a
synchronized block of code sees the effects of
all previous modifications that were guarded by
the same lock
synchronized(object) {
//do something...
…
}
23. Visibility problem
● There is no guarantee that the reading thread
will see a value written by another thread
● Using lock or volatile variable
24. Immutability
● Immutable object is always thread-safe
– Its state cannot be modified after construction
– All its fields are final
– It is properly constructed
(object doesn't escape during construction)
● Even when synchronization is not used
to publish the object reference
25. Safe publication
● Objects that are not immutable must be safely
published
● A properly constructed object can be safely
published by:
– Initializing an object reference form static initializer
– Storing into volatile or AtomicReference
– Storing into a final field of properly
constructed object
– Storing into a field properly guarded by
lock
26. Java Memory Model(JSR-133)
● Defines the semantics of multithreaded
programs
– Ensure your program run on all processor
architecture
● Happens-before
– If no, JVM is free to reorder
● New guarantees for Volatile
● Initialization Safety
– final
28. Happens-before
● Finalizer
● Some class libraries
– Concurrent containers
● Transitivity
– A -> B ,B -> C then A -> C
29. Volatile
Map configOptions;
volatile boolean initialized = false;
// In Thread A
configOptions = new HashMap();
ConfigOptions.put();
initialized = true;
// In Thread B
while (!initialized)
sleep();
// use configOptions
30. Initialization Safety
● When object is properly constructed, then all
threads will see the values for its final fields
that were set in its constructor, regardless of
whether or not synchronization is used
● Similar to a happens-before relationship
between the write of a final field in a
constructor and the initial load of a
shared reference to that object in
another thread
31. Executor framework
● If we have lots of tasks with threads, we need
to consider:
– How many thread should we create?
– How to stop them?
– What happened when a task failed?
32. Executor framework
● Executor manages running tasks
– Submit a Runnable to be run with
Executor#execute()
final ExecutorService executor = ...;
executor.execute(new Runnable(){
@Override
public void run(){
// do the task
}
});
33. Task cancellation
● Using interruption
public class Thread{
public void interrupt(){}
public boolean isInterrupted(){}
public static boolean interrupted(){}
}
● Responding to interruption
– throw exception again
– set interruption status
34. Non-interruptible block
● Synchronous Socket IO
– Close socket
● Lock
– Using explicit Lock and lockInterruptibly
36. Atomic*
● Lock-free thread-safe on single variable
● AtomicInteger, AtomicLong, AtomicReference
, etc.
– getAndAdd, addAndGet, incrementAndGet,
decrementAndGet, compareAndSet, etc.
● AtomicStampedReference,
AtomicMarkableReference
– ABA problem
37. Lock
interface Lock {
void lock();
void unlock();
…
}
● Only one thread can hold a lock at once
● ReentrantLock
– Can be reacquired by same thread
– Other threads can't acquire lock until
has been released same number of
times has been acquired
41. ConcurrentHashMap
● We love HashMap
– An easy to use Key-Value store
● Some new methods aware concurrency
– putIfAbsent
– remove(key, value)
– replace(key, value)
– replace(key, old value, new value)
42. JDK7 Fork/Join
● Fine-Grain Parallelism
1.Divide large task into small tasks
2.Process each task in separate thread
3.Join results
● ForkJoinPool
● ForkJoinTask
– RecursiveTask
– RecursiveAction
44. JDK8 ParallelIterable
public interface ParallelIterable<T> ... {
void forEach(Block<? super T> block)...
...
}
● Based on Fork/Join
● More elegant with lambda
users.parallel().forEach( u -> {...});
users.parallel().sorted( (u1, u2) ->
u1.name.compareTo(u2.name));
45. Useful tools
● ps -eLF
– show thread and process information
● jstack, jcmd
– command line, useful on server environment
● Jconsole, VisualVM
– visual tool integrate commands and tools
48. More...
● To utilize multiprocessor, which one is better?
– Thread or Process
● Performance
– How many thread is enough? Or only one thread?
● Other language or platform
– Python, Ruby, C, Node.js, .Net, etc.
49. Reference
● WIKIPEDIA Thread
– http://en.wikipedia.org/wiki/Thread_(computing)
● Extending the Haskell Foreign Function
Interface with Concurrency
– Simon Marlow, Simon Peyton Jones, and Wolfgang
Thaller, Haskell workshop 2004.
● JSR-133
● http://www.cs.umd.edu/~pugh/java/memoryMod
el/jsr-133-faq.html#volatile
50. Reference
● Java Concurrency in Practice
● Performance of Multi-Process and Multi-
ThreadProcessing on Multi-core SMT
Processors
– https://www.research.ibm.com/trl/people/inouehrs/p
df/IISWC2010_inoue_slides.pdf
● Java Technology on the Linux Platform
– http://java.sun.com/developer/
technicalArticles/Programming/linux/
● http://hg.openjdk.java.net/
51. Reference
● Java theory and practice: Fixing the Java
Memory Model, Part 2
– http://www.ibm.com/developerworks/library/j-
jtp03304/
● Programming with POSIX Threads
● Kernel Programming Guide(OS X)
– https://developer.apple.com/library/mac/#document
ation/Darwin/Conceptual/KernelProgramming/Mach
/Mach.html