## Full text

(1)

### Hsuan-Tien Lin

Deptartment of CSIE, NTU

### OOP Class, June 9, 2009

H.-T. Lin (NTU CSIE) Threads: the Stories OOP(even) 06/09/2009 0 / 19

(2)

## Story of a Bank: Part I

### Once upon a time, a bank uses the following system to allow customers to spend in local stores easily

1 l o c a l c r e d i t = g e t C r e d i t ( customer ) ;

2 tospend = g e t P r i c e ( i t e m ) ;

3 i f ( tospend <= l o c a l c r e d i t ) {

4 n e w c r e d i t = l o c a l c r e d i t − tospend ;

5 n o t i f y N e w C r e d i t ( n e w c r e d i t ) ;

6 }

H.-T. Lin (NTU CSIE) Threads: the Stories OOP(even) 06/09/2009 1 / 19

(3)

## Story of a Bank: Part II

### Normally,

1 l o c a l c r e d i t 1 = g e t C r e d i t ( customer1 ) ; / / 10000

2 tospend1 = g e t P r i c e ( i te m1 ) ; / / 3000

3 i f ( tospend1 <= l o c a l c r e d i t 1 ) {

4 n e w c r e d i t 1 = l o c a l c r e d i t 1 − tospend1 ; / / 2000

5 n o t i f y N e w C r e d i t ( n e w c r e d i t 1 ) ;

6 }

7 l o c a l c r e d i t 2 = g e t C r e d i t ( customer2 ) ; / / 10000

8 tospend2 = g e t P r i c e ( i te m2 ) ; / / 2000

9 i f ( tospend2 <= l o c a l c r e d i t 2 ) {

10 n e w c r e d i t 2 = l o c a l c r e d i t 2 − tospend2 ; / / 8000

11 n o t i f y N e w C r e d i t ( n e w c r e d i t 2 ) ;

12 }

H.-T. Lin (NTU CSIE) Threads: the Stories OOP(even) 06/09/2009 2 / 19

(4)

## Story of a Bank: Part III

### Unfortunately, customer 1 and 2 share the same account but go to different stores

1 l o c a l c r e d i t 1 = g e t C r e d i t ( customer1 ) ; / / 10000

2 l o c a l c r e d i t 2 = g e t C r e d i t ( customer2 ) ; / / 10000

3 tospend1 = g e t P r i c e ( i te m1 ) ; / / 3000

4 i f ( tospend1 <= l o c a l c r e d i t 1 ) {

5 n e w c r e d i t 1 = l o c a l c r e d i t 1 − tospend1 ; / / 7000

6 n o t i f y N e w C r e d i t ( n e w c r e d i t 1 ) ;

7 }

8 tospend2 = g e t P r i c e ( i te m2 ) ; / / 2000

9 i f ( tospend2 <= l o c a l c r e d i t 2 ) {

10 n e w c r e d i t 2 = l o c a l c r e d i t 2 − tospend2 ; / / 8000

11 n o t i f y N e w C r e d i t ( n e w c r e d i t 2 ) ;

12 }

13 g e t C r e d i t ( customer1 ) ; / / 8000

14 g e t C r e d i t ( customer2 ) ; / / 7000

H.-T. Lin (NTU CSIE) Threads: the Stories OOP(even) 06/09/2009 3 / 19

(5)

## Local copies are not trustworthy. Must update global copy atomically

H.-T. Lin (NTU CSIE) Threads: the Stories OOP(even) 06/09/2009 4 / 19

(6)

## An Example with Counter Threads

H.-T. Lin (NTU CSIE) Threads: the Stories OOP(even) 06/09/2009 5 / 19

(7)

## Story of a Couple: Part I

### Once upon a time, a couple shares a credit card account. To prevent overdraft, they agreed on the following protocol for using the credit card:

1 tospend = g e t P r i c e ( i t e m ) ;

2 c u r r e n t l i m i t = c h e c k C r e d i t b y C e l l p h o n e ( ) ;

3 i f ( tospend <= c u r r e n t l i m i t )

4 d o _ t r a n s a c t i o n ( ) ; / / a t o m i c a l l y

H.-T. Lin (NTU CSIE) Threads: the Stories OOP(even) 06/09/2009 6 / 19

(8)

## Story of a Couple: Part II

### Normally,

1 tospend = g e t P r i c e ( i t e m ) ; / / by George : 50000

2 c u r r e n t l i m i t = c h e c k C r e d i t b y C e l l p h o n e ( ) ; / / 60000

3 i f ( tospend <= c u r r e n t l i m i t ) / / by Mary : yes

4 d o _ t r a n s a c t i o n ( ) ; / / a t o m i c a l l y

5 tospend = g e t P r i c e ( i t e m ) ; / / by Mary : 20000

6 c u r r e n t l i m i t = c h e c k C r e d i t b y C e l l p h o n e ( ) ; / / 10000

7 i f ( tospend <= c u r r e n t l i m i t ) / / by Mary : no

8 d o _ t r a n s a c t i o n ( ) ; / / a t o m i c a l l y

H.-T. Lin (NTU CSIE) Threads: the Stories OOP(even) 06/09/2009 7 / 19

(9)

## Story of a Couple: Part III

### Unfortunately,

1 tospend = g e t P r i c e ( i t e m ) ; / / by George : 50000

2 c u r r e n t l i m i t = c h e c k C r e d i t b y C e l l p h o n e ( ) ; / / 60000

3 / / George d r i v e s t o t h e s t o r e

4 tospend = g e t P r i c e ( i t e m ) ; / / by Mary : 20000

5 c u r r e n t l i m i t = c h e c k C r e d i t b y C e l l p h o n e ( ) ; / / 60000

6 i f ( tospend <= c u r r e n t l i m i t ) / / by Mary : yes

7 d o _ t r a n s a c t i o n ( ) ;

8 i f ( tospend <= c u r r e n t l i m i t ) / / by George : yes

9 d o _ t r a n s a c t i o n ( ) ; / / OVERDRAFT ! !

H.-T. Lin (NTU CSIE) Threads: the Stories OOP(even) 06/09/2009 8 / 19

(10)

## Spent should happen immediately after checking

H.-T. Lin (NTU CSIE) Threads: the Stories OOP(even) 06/09/2009 9 / 19

(11)

## An Example with Couple Threads

H.-T. Lin (NTU CSIE) Threads: the Stories OOP(even) 06/09/2009 10 / 19

(12)

## Synchronization

### flexible, fine-grained use

H.-T. Lin (NTU CSIE) Threads: the Stories OOP(even) 06/09/2009 11 / 19

(13)

## More on the Lock

### lock releases after the method/block finishes (by return or exception)

H.-T. Lin (NTU CSIE) Threads: the Stories OOP(even) 06/09/2009 12 / 19

(14)

## An Example with Counter Threads

H.-T. Lin (NTU CSIE) Threads: the Stories OOP(even) 06/09/2009 13 / 19

(15)

## An Example with Couple Threads

H.-T. Lin (NTU CSIE) Threads: the Stories OOP(even) 06/09/2009 14 / 19

(16)

## A Story of the Black and White Goats: Deadlock

H.-T. Lin (NTU CSIE) Threads: the Stories OOP(even) 06/09/2009 15 / 19

(17)

## A Story of the Black and White Goats: Starvation

H.-T. Lin (NTU CSIE) Threads: the Stories OOP(even) 06/09/2009 16 / 19

(18)

## A Story of the Black and White Goats: Livelock

H.-T. Lin (NTU CSIE) Threads: the Stories OOP(even) 06/09/2009 17 / 19

(19)

## Ways to be Polite

1 s y n c h r o n i z e d v o i d make_payment (i n t amount ) {

2 w h i l e( no_money ( ) ) {

3 t h i s. complain ( 1 0 0 0 ) ;

4 }

5 }

1 s y n c h r o n i z e d v o i d make_payment (i n t amount ) {

2 w h i l e( no_money ( ) ) {

3 Thread . s l e e p ( 1 0 0 0 ) ;

4 }

5 }

6 / / . . .

1 s y n c h r o n i z e d v o i d make_payment (i n t amount ) {

2 w h i l e( no_money ( ) ) {

3 t h i s. w a i t ( 1 0 0 0 ) ;

4 }

5 }

6 / / . . .

H.-T. Lin (NTU CSIE) Threads: the Stories OOP(even) 06/09/2009 18 / 19

(20)

## Wait until Notified

1 s y n c h r o n i z e d v o i d make_payment (i n t amount ) {

2 w h i l e( no_money ( ) ) {

3 t h i s. w a i t ( ) ;

4 }

5 }

6

7 s y n c h r o n i z e d v o i d get_money (i n t amount ) {

8 money += amount ;

9 n o t i f y A l l ( ) ;

10 }

H.-T. Lin (NTU CSIE) Threads: the Stories OOP(even) 06/09/2009 19 / 19

Updating...

## References

Related subjects :