Docsity

Java Concurrency: BoundedBuffer и AtomicStampedReference, Study notes of Computer Science

Toggle sidebar
bg1
  
  
 
 
  
  
    
     
!
" " #
$%$ &%
'

   
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Java Concurrency: BoundedBuffer и AtomicStampedReference and more Study notes Computer Science in PDF only on Docsity!



 





 





  !"  !  !

Thread, synchronized, wait(),notify()…

$ 

 

java.util.concurrent

 % " 



" !&

 '   (

) 

(

*  



+" , 

" !&-

 "

 .

   ()  *

 "

"  

"  

java.nio





) 



  











java.util.concurrent.locks



4





" !&

 *!

lock(),

unlock()

tryLock()

tryLock(time, unit)



ReentrantLock



ReentrantReadWriteLock



   " *" " 





 "  " $ 



Condition





 

 



 



class BoundedBuffer {

final Lock

lock

=

new ReentrantLock();

final Condition

notFull

=

lock.newCondition();

final Condition

notEmpty

=

lock.newCondition();

public void put (Object x)

throws InterruptedException {

lock.lock

();

try {

while (count == items.length)

notFull.await

();

items[putptr] = x;if (++putptr == items.length)

putptr = 0;

++count; notEmpty.signal

();

} finally {

lock.unlock

(); }

}

public Object take()

throws InterruptedException {

lock.lock

();

try {

while (count == 0)

notEmpty.await

();

Object x = items[takeptr];if (++takeptr == items.length)

takeptr = 0;

--count; notFull.signal

();

return x;

} finally {

lock.unlock

(); } }

}









 

 





AtomicInteger

set(int)

get()

addAndGet(int),

incrementAndGet()

getAndAdd(int),

getAndIncrement()

compareAndSet(expected,update)

2 " 

!



 

 "   , ,  

 

** !  



 778 !,  

 

+ 

**  !!* 



" 

" !& , "  "  

*" 









 

 





AtomicReference



set(V newValue)



get()



compareAndSet(expected,update)



getAndSet(newValue)

9   " 

,  

 







  



 









  % :# ;

% 7< , =

# > 87%

9    



2

:< , (

% (

# ;



 "  

, -  $ . / - , (0  /

9    





2

:% , (

% (

 8;

9   %



 



+    ) 

 







  



 









  % :# ;

% 7< , =

# > 87%

9    



2

:< , (

% (

# ;



 "  

, -  $ . / - , (0  /

9    





2

:% , (

% (

 8;

9   %



 



+    ) 

 









  















  % :# ;

% 7< , =

# > 87%

9    



2

:< , (

% (

# ;



 "  

, -  $ . / - , (0  /

9    





2

:% , (

% (

 8;

9   %



 



+    ) 

 









  















  % :# ;

% 7< , =

# > 87%

9    



2

:< , (

% (

# ;



 "  

% 7< , = 87% > 8

9    





 1, $ 2

, 2

 /

9   %



 



+    ) 

 



 







3  " 

 

"  ?  

" 4

2 " !



%4

     ) 

$ 

% *



AtomicStampedReference

set(V

newReference, int

newStamp)

get(stampHolder)

compareAndSet(expectedRef,

newRef,expectedStamp,newStamp)



"  ) 

" !&

 "  

"     " 

 

!% 

! 

8@(

)   " A) 











  



 









  % :# ;

% 7< , =

# > 87%

9    



2

:< , (

% (

# ;



 "  

, -  $ . / - , (0  /

9    





2

:% , (

% (

 8;

9   %

 















 :" *B

A;   " 

   ()  *

 " 

5

 , " % 

" $ 

 



1

3



"   , ) )  **



%   







 





!" 











  '(   $ )  



2   , " *



"  , 

&

0  

 

" "  

"  



2 !!" )  **

 " 7*





"  

" 



  

*%  , 

!!



" *



 1

3



 % C