Programming with Threads in Java
-
Upload
koji-lin -
Category
Technology
-
view
2.233 -
download
2
description
Transcript of Programming with Threads in Java
![Page 1: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/1.jpg)
Programming with Threads in Java
koji lin@twjug 2012/9/15
![Page 2: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/2.jpg)
java.lang.Thread
![Page 3: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/3.jpg)
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
![Page 4: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/4.jpg)
On a computer with multiprocessors, processes or threads can run on different processors
~MSDN Threads and Processes
![Page 5: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/5.jpg)
其實只想講上面兩段 ,結束(誤)
![Page 6: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/6.jpg)
Thread Basics
![Page 7: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/7.jpg)
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
![Page 8: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/8.jpg)
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
![Page 9: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/9.jpg)
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
![Page 10: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/10.jpg)
Thread and Process
![Page 11: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/11.jpg)
Benefits of Thread
![Page 12: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/12.jpg)
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
![Page 13: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/13.jpg)
Multithreading Models
● User Level Threading– Many-to-One(N:1)
– Green Threads, GNU Portable Threads
● Kernel Level Threading– One-to-One(1:1)
– FreeBSD, Linux, Windows, Mac, Solaris...
● Hybrid– Many-to-Many(M:N)
– Solaris(before 9), Tru64 Unix
![Page 14: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/14.jpg)
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)
![Page 15: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/15.jpg)
How JVM creates thread?
● Thread.java– Start0 invokes StartThread
● jvm.cc– VM_ENTRY(void, JVM_StartThread(JNIEnv* env, jobject
jthread)) invokes JavaThread
● Thread.cpp– JavaThread::JavaThread invokes os::create_thread
● os_windows.cpp, os_linux.cpp, os_bsd.cpp– Win32 Thread,NPTL, libthr
![Page 16: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/16.jpg)
Does JVM do something special?
![Page 17: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/17.jpg)
No!!
![Page 18: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/18.jpg)
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
![Page 19: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/19.jpg)
Risks
● Safety Hazards– synchronization
● Liveness Hazards– deadlock
– starvation
● Performance Hazards– context switch
– synchronization
![Page 20: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/20.jpg)
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
![Page 21: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/21.jpg)
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++;
}
![Page 22: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/22.jpg)
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... …}
![Page 23: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/23.jpg)
Visibility problem
● There is no guarantee that the reading thread will see a value written by another thread
● Using lock or volatile variable
![Page 24: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/24.jpg)
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
![Page 25: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/25.jpg)
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 bylock
![Page 26: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/26.jpg)
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
![Page 27: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/27.jpg)
Happens-before
● Program order● Monitor lock
– explicit Lock object
● Volatile variable– AtomicXXX
● Thread start, termination● Interruption
![Page 28: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/28.jpg)
Happens-before
● Finalizer● Some class libraries
– Concurrent containers
● Transitivity– A -> B ,B -> C then A -> C
![Page 29: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/29.jpg)
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
![Page 30: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/30.jpg)
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
![Page 31: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/31.jpg)
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?
![Page 32: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/32.jpg)
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
}
});
![Page 33: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/33.jpg)
Task cancellation
● Using interruption
● Responding to interruption– throw exception again
– set interruption status
public class Thread{
public void interrupt(){}
public boolean isInterrupted(){}
public static boolean interrupted(){}
}
![Page 34: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/34.jpg)
Non-interruptible block
● Synchronous Socket IO– Close socket
● Lock– Using explicit Lock and lockInterruptibly
![Page 35: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/35.jpg)
java.util.concurrent.*
● Atomic*● Lock
– ReentrantLock
– ReadWrtieLock
● CountDownLatch● Semaphore● ConcurrentHashMap● Fork/Join (Java SE 7)
![Page 36: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/36.jpg)
Atomic*
● Lock-free thread-safe on single variable● AtomicInteger, AtomicLong, AtomicReference
, etc.– getAndAdd, addAndGet, incrementAndGet,
decrementAndGet, compareAndSet, etc.
● AtomicStampedReference, AtomicMarkableReference– ABA problem
![Page 37: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/37.jpg)
Lock
● 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
interface Lock { void lock(); void unlock(); …}
![Page 38: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/38.jpg)
ReadWriteLock(1)
● Readers-writers problem
– 同時有複數個讀與寫的動作想要執行 ,當有寫入動作時,其他讀寫都不能執行;而沒有寫入動作時,則可同時執行多個讀取。
● Writer starvation/Writer preference● Fair/Unfair mode
– 是否依照抵達順序– 實作上看似仍會去避免無限期延遲的狀況
![Page 39: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/39.jpg)
ReadWriteLock(2)
● Fair - 當 reader取得 lock後有 writer在等待,那麼之後的 reader將會等到該 writer取得並釋放後才能取得。
● Unfair -當 Queue中沒有 reader時,行為同上 ;但是當新的 reader到達時,還有 reader在deque,則新 reader會跳過等待的writer先執行。 (bug id:6816565)
![Page 40: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/40.jpg)
Semaphore
● Counting Semaphore
● 用於管理有限資源存取– 例如 Pool
● acquire(), tryAcquire()
– 當計數不為 0時,內部計數減1並允許執行– 如果計數為 0則等待直到計數不為 0
● release()
– 內部計數加1
![Page 41: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/41.jpg)
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)
![Page 42: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/42.jpg)
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
![Page 43: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/43.jpg)
JDK7 Fork/Join
● Work Stealing
![Page 44: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/44.jpg)
JDK8 ParallelIterable
● Based on Fork/Join● More elegant with lambda
public interface ParallelIterable<T> ... {
void forEach(Block<? super T> block)...
...
}
users.parallel().forEach( u -> {...});
users.parallel().sorted( (u1, u2) -> u1.name.compareTo(u2.name));
![Page 45: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/45.jpg)
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
![Page 46: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/46.jpg)
Is multithreaded programming
hard ?
![Page 47: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/47.jpg)
Yes
![Page 48: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/48.jpg)
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.
![Page 49: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/49.jpg)
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
![Page 50: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/50.jpg)
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/
![Page 51: Programming with Threads in Java](https://reader034.fdocuments.in/reader034/viewer/2022050815/546c925eaf795971298b4f92/html5/thumbnails/51.jpg)
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/#documentation/Darwin/Conceptual/KernelProgramming/Mach/Mach.html