Advertisement
Josif_tepe

Untitled

Mar 24th, 2022
668
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.84 KB | None | 0 0
  1. public class Main {
  2.     public static void main(String[] args) {
  3.         Brojac brojac = new Brojac();
  4.  
  5.         t t1 = new t(1, brojac);
  6.         t t2 = new t(2, brojac);
  7.     t1.start();
  8.     t2.start();
  9.     try {
  10.         t1.join();
  11.         t2.join();
  12.     }
  13.     catch (InterruptedException ex) {
  14.         ex.printStackTrace();
  15.     }
  16.         System.out.println(brojac.getCnt());
  17.  
  18.  
  19.  }
  20. }
  21. class t extends  Thread {
  22.     int id;
  23.     static Brojac br;
  24.     public t(int _id, Brojac br) {
  25.         id = _id;
  26.         this.br = br;
  27.     }
  28.  
  29.     @Override
  30.     public void run() {
  31.       for(int i = 0; i < 20; i++) {
  32.           br.zgolemi_brojac();
  33.       }
  34.     }
  35. }
  36. class Brojac  {
  37.     private static int cnt = 0;
  38.     public static void zgolemi_brojac() {
  39.         cnt++;
  40.     }
  41.     public static int getCnt() {
  42.         return cnt;
  43.     }
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement