Advertisement
Guest User

Laborki_wspolbiezne

a guest
Mar 29th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package edu.uz.instrukcja01.zad02;
  2.  
  3. public class LicznikWatkow extends Thread {
  4. private int wartoscKrytyczna;
  5.  
  6. public int policzMnie () {
  7. wartoscKrytyczna += 1;
  8. wartoscKrytyczna--;
  9. return wartoscKrytyczna;
  10. }
  11. }
  12.  
  13.  
  14.  
  15.  
  16. public class WatekLiczacy extends Thread{
  17. private LicznikWatkow licznikWatkow; private int count;
  18.  
  19. public WatekLiczacy(String name, LicznikWatkow licznikWatkow, int count) {
  20. super(name); this.licznikWatkow = licznikWatkow;
  21. this.count = count;
  22. }
  23.  
  24. public void run() {
  25. int wynik = 0;
  26.  
  27. for(int i=0; i< count; i++) {
  28. wynik = licznikWatkow.policzMnie();
  29. if(wynik != 0) break;
  30. }
  31.  
  32. System.out.println(Thread.currentThread().getName() + " konczy wynikiem " + wynik);
  33. }
  34. }
  35.  
  36.  
  37. public class TestWatkow {
  38. public static void main(String[] args) {
  39.  
  40. int tnum = Integer.parseInt(args[0]);
  41. int count = Integer.parseInt(args[1]);
  42.  
  43. LicznikWatkow licznik = new LicznikWatkow();
  44.  
  45. WatekLiczacy[] thread = new WatekLiczacy[tnum];
  46. for (int i = 0; i<tnum; i++)
  47. thread[i] = new WatekLiczacy("Watek" + (i+1), licznik, count);
  48.  
  49. try{
  50. for (int i=0; i< tnum; i++) {thread[i].join();}
  51. } catch (InterruptedException exc) { System.exit(1); }
  52. System.out.println("Koniec programu");
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement