Download presentation
Presentation is loading. Please wait.
1
Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)
2
Threads Advantages some tasks inherently parallel – can exploit the underlying architecture responsive applications – user can still use application while operation pending sharing of resources (communication) is easier switching between threads is faster than process switching overhead in creating threads is small
3
Threads Single-threaded process Global DataFiles, I/O RegistersStack thread Multi-threaded process Global DataFiles, I/O Registers Stack Registers Stack Registers Stack
4
Java thread States newrunnabledead blocked new start() exits run() method wait for I/O (file, input) I/O available
5
Creating Threads Extend the Thread class, override run() method class FetchThread extends Thread { private String url; public FetchThread(String url) { this.url = url; } public void run() { // 1. connect to server // 2. download page // 3. update view widget } // LATER ON... Thread fetch = new FetchThread(“www.google.com”); fetch.start();
6
Creating Threads Implement Runnable interface (only one method run()) class FetchThread implements Runnable { private String url; public FetchThread(String url) { this.url = url; } public void run() { // 1. connect to server // 2. download page // 3. update view widget } // LATER ON... Runnable target = new FetchThread(“www.google.com”); Thread fetch = new Thread(target); fetch.start();
7
Java thread States newrunnabledead blocked new start() exits run() method wait for I/O (file, input) I/O available
8
Race Condition class Demo { private int i = 0; class DemoThread implements Thread { public void run() { i = i + 1; } public void test() { DemoThread t1 = new DemoThread(); DemoThread t2 = new DemoThread(); t1.start(); t2.start(); // What is the value of i? }
9
Race Condition i = i + 1 t1 t2 i : 0
10
Race Condition # i = i + 1 1. LOD $i # load value of i in accumulator 2. INC # increment value of accumulator 3. STO $i # store accumulator in i t1 t2 i : 0
11
Race Condition # i = i + 1 1. LOD $i # load value of i in accumulator 2. INC # increment value of accumulator 3. STO $i # store accumulator in i t1 t2 PC : 1 A : ? PC : 1 A : ? i : 0
12
Race Condition # i = i + 1 1. LOD $i # load value of i in accumulator 2. INC # increment value of accumulator 3. STO $i # store accumulator in i t1 t2 PC : 1 A : ? PC : 1 A : ? i : 0
13
Race Condition # i = i + 1 1. LOD $i # load value of i in accumulator 2. INC # increment value of accumulator 3. STO $i # store accumulator in i t1 t2 PC : 2 A : 0 PC : 1 A : ? i : 0
14
Race Condition # i = i + 1 1. LOD $i # load value of i in accumulator 2. INC # increment value of accumulator 3. STO $i # store accumulator in i t1 t2 PC : 2 A : 0 PC : 1 A : ? i : 0
15
Race Condition # i = i + 1 1. LOD $i # load value of i in accumulator 2. INC # increment value of accumulator 3. STO $i # store accumulator in i t1 t2 PC : 2 A : 0 PC : 1 A : ? i : 0
16
Race Condition # i = i + 1 1. LOD $i # load value of i in accumulator 2. INC # increment value of accumulator 3. STO $i # store accumulator in i t1 t2 PC : 2 A : 0 PC : 2 A : 0 i : 0
17
Race Condition # i = i + 1 1. LOD $i # load value of i in accumulator 2. INC # increment value of accumulator 3. STO $i # store accumulator in i t1 t2 PC : 2 A : 0 PC : 2 A : 0 i : 0
18
Race Condition # i = i + 1 1. LOD $i # load value of i in accumulator 2. INC # increment value of accumulator 3. STO $i # store accumulator in i t1 t2 PC : 2 A : 0 PC : 3 A : 1 i : 0
19
Race Condition # i = i + 1 1. LOD $i # load value of i in accumulator 2. INC # increment value of accumulator 3. STO $i # store accumulator in i t1 t2 PC : 2 A : 0 PC : 3 A : 1 i : 0
20
Race Condition # i = i + 1 1. LOD $i # load value of i in accumulator 2. INC # increment value of accumulator 3. STO $i # store accumulator in i t1 t2 PC : 2 A : 0 PC : 4 A : 1
21
Race Condition # i = i + 1 1. LOD $i # load value of i in accumulator 2. INC # increment value of accumulator 3. STO $i # store accumulator in i t1 t2 PC : 2 A : 0 PC : 4 A : 1
22
Race Condition # i = i + 1 1. LOD $i # load value of i in accumulator 2. INC # increment value of accumulator 3. STO $i # store accumulator in i t1 t2 PC : 2 A : 0 PC : 4 A : 1
23
Race Condition # i = i + 1 1. LOD $i # load value of i in accumulator 2. INC # increment value of accumulator 3. STO $i # store accumulator in i t1 t2 PC : 3 A : 1 PC : 4 A : 1
24
Race Condition # i = i + 1 1. LOD $i # load value of i in accumulator 2. INC # increment value of accumulator 3. STO $i # store accumulator in i t1 t2 PC : 3 A : 1 PC : 4 A : 1
25
Race Condition # i = i + 1 1. LOD $i # load value of i in accumulator 2. INC # increment value of accumulator 3. STO $i # store accumulator in i t1 t2 PC : 4 A : 1 PC : 4 A : 1
26
Race Condition # i = i + 1 1. LOD $i # load value of i in accumulator 2. INC # increment value of accumulator 3. STO $i # store accumulator in i t1 t2 PC : 4 A : 1 PC : 4 A : 1
27
Race Condition (BankAccount Demo)
28
Race Condition Possible solution approaches synchronized keyword synchronized method -- guarantees that method will execute in its entirety if multiple threads attempt a call synchronized lock – can lock an object so that method calls are synchronized Lock objects fine-grained control over protecting sections of code
29
Race Condition Synchronized methods guarantee that method will execute in its entirety if other threads want to execute same method of same object, they are put in wait state synchronized public void someMethod() { }
30
Race Condition Synchronized lock on object public void someMethod() { synchronized ( myImportantVariable ) { modify / inspect myImportantVariable }
31
Race Condition Lock objects guarantee that a section of code will execute in its entirety if other threads want to execute same section, they are put in wait state give fine-grained control over protecting sections of code Locks can be shared among several threads and several code sections
32
Race Condition Lock objects Lock lock = new ReentrantLock(); // e.g. in constructor public void methodOne() { lock.lock(); try { critical section } finally { lock.unlock(); } public void methodTwo() { lock.lock(); try { critical section } finally { lock.unlock(); }
33
Race Condition (Fix BankAccount)
34
Producer / Consumer Problem General Setup: one shared resource producer generates values consumer process each value Goal: Ensure that each value processed only once no value is lost
35
Race Condition (Producer Consumer)
36
Race Condition Possible solution approaches Busy lopping consumer waits until resource available producer waits until resource unused Condition objects obtain a lock, but release immediately if condition not met other threads can attempt to acquire lock notify others when condition has been met
37
Race Condition (Fix Producer/consumer)
38
Dining Philosophers Problem
39
Possible solution Each philosopher attempts to: 1. acquire left fork 2. acquire right fork 3. eat 4. release right fork 5. release left fork
40
Dining Philosophers Demo
41
Synchronized Data Structures ArrayList is not thread-safe Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list. If no such object exists, the list should be "wrapped" using the Collections.synchronizedList method.
42
Synchronized Data Structures ArrayList is not thread-safe Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list. If no such object exists, the list should be "wrapped" using the Collections.synchronizedList method. To make thread-safe List list = Collections.synchronizedList(new ArrayList(...)); (Same API as ArrayList --- ArrayList implements List)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.