threads load new page page is loading browser still responds to user (can read pages in other tabs)

42
Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

Post on 19-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

Threads

Load new page

Page is loading

Browser still responds to user(can read pages in other tabs)

Page 2: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

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

Page 3: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

ThreadsSingle-threaded process

Global Data Files, I/O

Registers Stack

thread

Multi-threaded process

Global Data Files, I/O

Registers

Stack

Registers

Stack

Registers

Stack

Page 4: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

Java thread States

new runnable dead

blocked

new start() exits run()method

wait for I/O(file, input)

I/O available

Page 5: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

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();

Page 6: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

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();

Page 7: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

Java thread States

new runnable dead

blocked

new start() exits run()method

wait for I/O(file, input)

I/O available

Page 8: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

Race Conditionclass 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?}

}

Page 9: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

Race Condition

i = i + 1

t1 t2

i : 0

Page 10: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

Race Condition

# i = i + 11. 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

Page 11: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

Race Condition

# i = i + 11. LOD $i # load value of i in accumulator

2. INC # increment value of accumulator

3. STO $i # store accumulator in i

t1 t2PC : 1A : ?

PC : 1A : ?

i : 0

Page 12: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

Race Condition

# i = i + 11. LOD $i # load value of i in accumulator

2. INC # increment value of accumulator

3. STO $i # store accumulator in i

t1 t2PC : 1A : ?

PC : 1A : ?

i : 0

Page 13: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

Race Condition

# i = i + 11. LOD $i # load value of i in accumulator

2. INC # increment value of accumulator

3. STO $i # store accumulator in i

t1 t2PC : 2A : 0

PC : 1A : ?

i : 0

Page 14: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

Race Condition

# i = i + 11. LOD $i # load value of i in accumulator

2. INC # increment value of accumulator

3. STO $i # store accumulator in i

t1 t2PC : 2A : 0

PC : 1A : ?

i : 0

Page 15: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

Race Condition

# i = i + 11. LOD $i # load value of i in accumulator

2. INC # increment value of accumulator

3. STO $i # store accumulator in i

t1 t2PC : 2A : 0

PC : 1A : ?

i : 0

Page 16: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

Race Condition

# i = i + 11. LOD $i # load value of i in accumulator

2. INC # increment value of accumulator

3. STO $i # store accumulator in i

t1 t2PC : 2A : 0

PC : 2A : 0

i : 0

Page 17: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

Race Condition

# i = i + 11. LOD $i # load value of i in accumulator

2. INC # increment value of accumulator

3. STO $i # store accumulator in i

t1 t2PC : 2A : 0

PC : 2A : 0

i : 0

Page 18: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

Race Condition

# i = i + 11. LOD $i # load value of i in accumulator

2. INC # increment value of accumulator

3. STO $i # store accumulator in i

t1 t2PC : 2A : 0

PC : 3A : 1

i : 0

Page 19: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

Race Condition

# i = i + 11. LOD $i # load value of i in accumulator

2. INC # increment value of accumulator

3. STO $i # store accumulator in i

t1 t2PC : 2A : 0

PC : 3A : 1

i : 0

Page 20: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

Race Condition

# i = i + 11. LOD $i # load value of i in accumulator

2. INC # increment value of accumulator

3. STO $i # store accumulator in i

t1 t2PC : 2A : 0

PC : 4A : 1

i : 00, 1

Page 21: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

Race Condition

# i = i + 11. LOD $i # load value of i in accumulator

2. INC # increment value of accumulator

3. STO $i # store accumulator in i

t1 t2PC : 2A : 0

PC : 4A : 1

i : 00, 1

Page 22: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

Race Condition

# i = i + 11. LOD $i # load value of i in accumulator

2. INC # increment value of accumulator

3. STO $i # store accumulator in i

t1 t2PC : 2A : 0

PC : 4A : 1

i : 00, 1

Page 23: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

Race Condition

# i = i + 11. LOD $i # load value of i in accumulator

2. INC # increment value of accumulator

3. STO $i # store accumulator in i

t1 t2PC : 3A : 1

PC : 4A : 1

i : 00, 1

Page 24: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

Race Condition

# i = i + 11. LOD $i # load value of i in accumulator

2. INC # increment value of accumulator

3. STO $i # store accumulator in i

t1 t2PC : 3A : 1

PC : 4A : 1

i : 00, 1

Page 25: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

Race Condition

# i = i + 11. LOD $i # load value of i in accumulator

2. INC # increment value of accumulator

3. STO $i # store accumulator in i

t1 t2PC : 4A : 1

PC : 4A : 1

i : 00, 11, 1

Page 26: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

Race Condition

# i = i + 11. LOD $i # load value of i in accumulator

2. INC # increment value of accumulator

3. STO $i # store accumulator in i

t1 t2PC : 4A : 1

PC : 4A : 1

i : 00, 11, 1

Page 27: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

Race Condition

(BankAccount Demo)

Page 28: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

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

Page 29: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

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()

{

}

Page 30: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

Race Condition

Synchronized lock on object

public void someMethod()

{

synchronized ( myImportantVariable )

{

modify / inspect myImportantVariable

}

}

Page 31: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

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

Page 32: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

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();

}

}

Page 33: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

Race Condition

(Fix BankAccount)

Page 34: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

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

Page 35: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

Race Condition

(Producer Consumer)

Page 36: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

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

Page 37: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

Race Condition

(Fix Producer/consumer)

Page 38: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

Dining Philosophers Problem

Page 39: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

Dining Philosophers Problem

Possible solution

Each philosopher attempts to:

1. acquire left fork

2. acquire right fork

3. eat

4. release right fork

5. release left fork

Page 40: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

Dining Philosophers Demo

Page 41: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

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.

Page 42: Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)

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)