Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- public static void main(String[] args) {
- Brojac brojac = new Brojac();
- Brojac brojac2 = new Brojac();
- t t1 = new t(1, brojac);
- t t2 = new t(2, brojac);
- t1.start();
- t2.start();
- try {
- t1.join();
- t2.join();
- }
- catch (InterruptedException ex) {
- ex.printStackTrace();
- }
- System.out.println(brojac.getCnt());
- // System.out.println(brojac2.getCnt());
- }
- }
- class t extends Thread {
- int id;
- static Brojac br;
- public t(int _id, Brojac br) {
- id = _id;
- this.br = br;
- }
- @Override
- public void run() {
- for(int i = 0; i < 20; i++) {
- br.zgolemi_brojac_safe();
- }
- }
- }
- class Brojac {
- private static int cnt = 0;
- // public static void zgolemi_brojac() {
- // cnt++;
- // }
- public synchronized void zgolemi_brojac_safe() {
- cnt++;
- }
- public static int getCnt() {
- return cnt;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement