Advertisement
matbiz01

laby

Oct 9th, 2022
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. package lab1;
  2.  
  3.  
  4. import java.util.LinkedList;
  5. import java.util.Vector;
  6.  
  7. public class Main extends Thread {
  8.  
  9. public static Counter counter = new Counter();
  10. public static final int threadCount = 2;
  11. public static final int loopCount = 5000;
  12.  
  13. public static void main(String[] args) throws InterruptedException {
  14.  
  15. Vector<Main> threads = new Vector<>();
  16. for(int i = 0; i < threadCount; i++){
  17. threads.add(new Main());
  18. threads.get(i).start();
  19. }
  20.  
  21. for(int i = 0; i < threadCount; i++){
  22. threads.get(i).join();
  23. }
  24.  
  25. System.out.println("done");
  26. counter.print();
  27. }
  28.  
  29. public void run(){
  30.  
  31. for(int i = 0; i < loopCount; i++){
  32. counter.increment();
  33. counter.decrement();
  34. }
  35.  
  36. //counter.print();
  37. }
  38. }
  39.  
  40.  
  41.  
  42.  
  43. package lab1;
  44.  
  45. public class Counter {
  46. private int value = 0;
  47.  
  48. public void increment(){
  49. value++;
  50. }
  51.  
  52. public void decrement(){
  53. value--;
  54. }
  55.  
  56. public void print(){
  57. System.out.println(value);
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement