Advertisement
sergAccount

Untitled

Mar 21st, 2021
711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.71 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package com.mycompany.app24;
  7.  
  8. /**
  9.  *
  10.  * @author Admin
  11.  */
  12. public class SynchronizedCounter {
  13.    
  14.     private int i = 0;
  15.  
  16.     // синхронизированный метод
  17.     // обеспечивает доступ только одного потока выполнения
  18.     // synchronized - ключевое слово
  19.     public synchronized void inc() {
  20.         i++;
  21.     }
  22.     public synchronized void dec(){
  23.         i--;
  24.     }
  25.     public synchronized int getValue(){
  26.         return i;
  27.     }
  28.    
  29. }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement