Docsity

Реализация атомарных счетчиков с помощью класса AtomicCounter в Java, Study notes of Computer Science

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

Partial preview of the text

Download Реализация атомарных счетчиков с помощью класса AtomicCounter в Java and more Study notes Computer Science in PDF only on Docsity!



  

 

   

    



         !  



     ! 



   

  



class AtomicCounter {

private int count;AtomicCounter (int n) {

count = n;

} public void increment() {

synchronized

(this) {

count++;

}

} public int getCount() {

int c; synchronized

(this) {

c = count;

} return c;

}

}



   

  



class AtomicCounter {

private int count;AtomicCounter (int n) {

count = n;

} synchronized

public void increment() {

count++;

} synchronized

public int getCount() {

int c;

c = count;

return c;

}

}



   



extend Thread



 

 

run()

&  )



 " ' & 

start()



   

class CountUp extends Thread {

public void run () {

counter.increment();System.out.println ("count = " +

counter.getCount());

} static AtomicCounter counter

= new AtomicCounter (0);

} public class testme {

public static void main (String args[]) {for (int i = 0; i < 100; i++) {

CountUp f = new CountUp();f.start();

}

}

   

class CountUp extends Thread {

public void run () {

synchronized

(counter) {

counter.increment();System.out.println ("count = " +

counter.getCount());

}

} static AtomicCounter counter

= new AtomicCounter (0);

} public class testme2 {

public static void main (String args[]) {for (int i = 0; i < 100; i++) {

CountUp f = new CountUp();f.start();

}

}

    !  "





"!

 !

 

Thread

 $ %

 4

& !)   5 ) !!





! /

 ! !

 ) 

class CountUp extends Thread {

public void run () {

synchronized (counter) {

counter.increment();System.out.println ("count = " +

counter.getCount());

}

} private

AtomicCounter counter

= new AtomicCounter (0);

}



   



 



 & ,& !) &  ) +



setPriority(int)



getPriority()



yield()

( & & !) 2  



t.join()

 !5 & !)     







$

 

$





2 "    5      



0 8

'

!

9,:



  )  )  & #!"!

;19;:



 

  (!<     

$ !



   & 

/  0

 "!

   $ 

 ' 

1   (



    5

!" 



    & !& ! 



9

,&  :



   $.  .' 

 $(



-)  " 



/ 

(



  (



) +   

(    



' 

( ) *  +



2

 0+ /  0

 " 3   





 '  

put()

55 



/



poll()

5 



 

 

 

  

 

!  & !) 



 +  .  (  

 !



2! 

!  

 !  +







' 

( ) *  -



$ )     (3   



! & 

put()

 !5 

take()



  + *&  ( )   



2! 

!  

 !  +







!  $  5& !) !!'



' 

( ) * .





 

$/  0

 " 3   



6  $    ) ) .   /

$ !)   & !



!) 4 & 

 

9  :



2! 

!  

 !  +

    

 





1  4