SHARE
TWEET

Untitled

a guest Dec 6th, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Counter {
  2.     private long c1 = 0; private long c2 = 0;
  3.  
  4.  
  5.     private Object lock1 = new Object();
  6.     private Object lock2 = new Object();
  7.  
  8.  
  9.     public void inc1() {
  10.         synchronized(lock1) {
  11.             try {
  12.             c1++;
  13.             Thread.sleep(1);
  14.             } catch (InterruptedException e) {
  15.         // some code
  16.             }
  17.         }
  18.     }
  19.    
  20.     public void inc2() {
  21.         synchronized(lock2) {
  22.             try{
  23.             c2++;
  24.             Thread.sleep(1);
  25.             } catch (InterruptedException e) {
  26.         // some code
  27.             }  
  28.         }
  29.     }
  30.  
  31.     public long get_c1() {
  32.     return(c1);
  33.     }
  34.  
  35.     public long get_c2() {
  36.     return(c2);
  37.     }
  38. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top