Multi Threading in Java
Multi Threading in Java
Jeff HUANG
Software Engineering Group @HKUST
Lets see
File
DB
Multi-threading
A thread is a single sequential flow of control In a single process, there could be multiple threads Threads share memory The server program spawns multiple threads Each thread handles a client request
5
Example
A single core CPU and a task
compute readfile compute readfile compute writefile compute
7 sec
2 such tasks
6
A simple accounting
Single thread
T=14
Two threads
T=8
Whats more
Multi-core & multi-processor
More accounting
Number of cores = 1,000,000 = 1M Multi-threading can do 2M such tasks in 8 seconds! What b t th i l th Wh t about the single-threaded version? d d i ?
More benefits
Performance User responsiveness p Resource utilization Simplicity f Si li it of modeling d li Simplified handling of asynchronous events p g y
10
12
13
14
Runnable interface
public interface Runnable { public void run(); // work thread }
15
Thread Class
public class Thread extends Object implements Runnable { public Thread(); public Thread(String name); // Thread name p public Thread(Runnable R); // Thread R.run() ( ); () public Thread(Runnable R, String name); public void run(); // if no R, work for thread bli id () R kf h d public void start(); // begin thread execution ... }
16
Example
public class MyThread extends Thread { public void run() { // work for thread } } MyThread M Th d T = new M Th d () ; // create th d MyThread t thread T.start(); // begin running thread // thread executing in parallel
18
Example E l
public class MyRunnable implements Runnable { public void run() { // work for thread } } MyRunnable myR = new MyRunnable(); // create runnable object Thread T = new Thread(myR); // create thread T.start(); // begin running thread // thread executing in parallel
19
Producer - Consumer
class Producer extends Thread { private final BlockingQueue queue; Producer(BlockingQueue q){queue = q;} public void run() { while(true) { queue.put(produce()); queue put(produce()); } } private Message produce() { return new Message(); } }
public static void main (String[] args) Consumer(BlockingQueue q) { queue = q; } { public void run() { BlockingQueue q = new SomeQueue(); while(true) { Producer p = new Producer(q); consume(queue.take()); consume(queue take()); Consumer c = new Consumer(q); } } p.start(); private void consume(Message mes) { c.start(); } }
}
Example 1
20
Simple WebServer
public class MyRunnable implements Runnable { public void run() { handleRequest(connection); Process request } } public class SimpleWebServer { public static void main (String[] args) throws IOException { ServerSocket socket = new ServerSocket(80); while (true) { Accept client request final Socket fi l S k t connection = socket.accept(); ti k t t() Runnable task = new MyRunnable(connection); Create Thread t = new Thread(task); Create thread Runnable t.start(); t start(); start th t t thread d } }
Example 2
21
22
Thread States
Java thread can be in one of these states
New Runnable Running Blocked Dead thread allocated & waiting for start() thread can begin execution thread currently executing thread waiting for event (I/O, etc.) thread finished
Thread States
new new
start runnable scheduler y yield, time slice notify, notifyAll, notify notifyAll IO complete, sleep expired
blocked
24
Thread Scheduling
Scheduler
Determines which runnable threads to run Can be based on thread priority Part of OS or Java Virtual Machine (JVM)
Scheduling policy
Preempted by scheduler
25
???
??? ???
main (): thread A, thread B thread A: println 1, println 2, println 3 thread B: println 1, println 2, println 3
27
29
Another example
public class Worker extends Thread { static int x = 0; public void run() { for (int i = 0; i < 1000; i++) { First increase x by 1 x = x + 1; Then decrease x by 1 x = x 1; } } public static void main (String[] args) { x = 0; for (int i = 0; i < 1000; i++) Start 1000 threads new Worker().start(); // wait for all threads exit System.out.println(x); } }
30
Data Race
public class Worker extends Thread { static int x = 0; public void run() { for (int i = 0; i < 1000; i++) { First increase x by 1 x = x + 1; Then decrease x by 1 x = x 1; } } public static void main (String[] args) { x = 0; for (int i = 0; i < 1000; i++) Start 1000 threads new Worker().start(); // wait for all threads exit System.out.println(x); } }
X is not always 0 !!
31
Quiz time
32
Answer: Yes!
33
The Java memory model is designed to allow aggressive optimization On multi-processor, values not synchronized to global memory Good for performance p
34
Observations
Multithread execution is non-deterministic
Depends on scheduler & single or multi-core
35
y Java synchronization
36
Java synchronization
Java uses the concept of monitors Java uses the concept that every object is i t d ith l k associated with a lock use synchronized keyword Two ways
Synchronized methods Synchronized blocks
37
Synchronized method
Calling a synchronized method attempts to possess the lock
If no one owns the lock, then this thread has/owns the lock and proceeds. Otherwise, it has to wait until the lock is released by some other thread
Lock
For static method, it is the lock associated with the Class object to which the static method belongs to For instance method, it is the lock associated with the bj t th object on which the method call is being made hi h th th d ll i b i d
38
Synchronized blocks
Very similar to synchronized method but it is method, Blocks of code, rather than entire methods
public void someMethod() { // non-critical section synchronized(someobject) { // critical section } // non-critical section }
synchronized(this)
39
Data Race
public class Worker extends Thread { static int x = 0; public void run() { for (int i = 0; i < 1000; i++) { First increase x by 1 x = x + 1; Then decrease x by 1 x = x 1; } } public static void main (String[] args) { x = 0; for (int i = 0; i < 1000; i++) Start 1000 threads new Worker().start(); // wait for all threads exit System.out.println(x); } }
X is not always 0 !!
40
Using synchronization
public class Worker extends Thread { static int x = 0; static Object lock = new Object(); j j public void run() { synchronized(lock) { for (int f (i t i = 0 i < 1000 i++) { 0; 1000; x = x + 1; x = x 1; } } } }
41
Questions
What would happen if the lock field were not static? Why dont we just make the run method synchronized? Why dont we just synchronize on x?
42
45
46
Deadlock
client1 holds lock for account a1, waits for account a2 client2 holds lock for account a2, waits for , account a1 Both of them can not proceed
Client1 Cli 1 Client2
a1
a2
47
Impair performance
Frequent lock acquire and lock release operations We are sacrificing performance for data safety But sometimes there is no conflict On some extreme situations, performance is greatly impaired
48
Is it hard?
49
50
Java synchronization
Syncrhonized methods/blocks
51
Questions
What would happen if the lock field were not static? Why dont we just make the run method synchronized? Why dont we just synchronize on x?
52
Using synchronization
public class Worker extends Thread { static int x = 0; static Object lock = new Object(); j j public void run() { synchronized(lock) { for (int f (i t i = 0 i < 1000 i++) { 0; 1000; x = x + 1; x = x 1; } } } }
53
Backups p
Daemon Threads
Java threads types
User Daemon
Provide general services Typically never terminate Call setDaemon() before start()
Program termination
1. All user threads finish 2. Daemon threads are terminated by JVM 3. Main program finishes p g
56
Atomicity violation
57
58
Non-preemptive Scheduling
Threads continue execution until
Thread terminates Executes instruction causing wait (e.g., IO) Thread volunteering to stop (invoking yield or sleep)
59
Preemptive Scheduling
Threads continue execution until
Same reasons as non-preemptive scheduling Preempted by scheduler
60
Transactional Memory
Instead of using synchronizations, the runtime system ensures that the critical section executes transactionally t ti ll You can just go ahead when entering a critical section; when you are exiting the critical section, the ti h iti th iti l ti th runtime system will check the data safety for you
if th there is no conflict, just proceed i fli t j t d Otherwise, rollback
61
62
Advantages
No deadlock!
we never refer to locks explicitly
63