Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. package lab12;
  2.  
  3. import java.util.concurrent.ExecutorService;
  4. import java.util.concurrent.Executors;
  5.  
  6. public class ModificateThreadVolatile implements Runnable {
  7. private String name;
  8.  
  9.  
  10.  
  11. static ModificateValueVolatile value = new ModificateValueVolatile();
  12.  
  13.  
  14. public ModificateThreadVolatile(String name) {
  15. this.name=name;
  16. }
  17. public void run() {
  18.  
  19. for (int it=0;it<10;it++){
  20. value.increaseValue();
  21. System.out.println(name+":"+value.getValue());
  22. value.decreaseValue();
  23.  
  24. System.out.println(name+":"+value.getValue());
  25. }
  26. }
  27. public static void main(String[] args) {
  28. ExecutorService executor=Executors.newCachedThreadPool();
  29. executor.execute(new ModificateThreadVolatile("Watek 1"));
  30. executor.execute(new ModificateThreadVolatile("Watek 2"));
  31. }
  32.  
  33. package lab12;
  34.  
  35. public class ModificateValueVolatile {
  36. private volatile long value;
  37.  
  38. public long getValue() {
  39. return value;
  40. }
  41.  
  42. public void increaseValue() {
  43. this.value++;
  44. }
  45. public void decreaseValue() {
  46. this.value--;
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement