java - Coordinate multiple threads after N executions -


i have java/groovy multi threaded process want "synchronize" after n executions: - shared counter decremented after each thread execution - goal reset counter once reaches 0 while no other thread accessing it.

i tried readwritereentrantlock looks have race condition decrement phase. here test code

public static void main(string[] args) {     atomicinteger counter = new atomicinteger(decrementer.max_size)     readwritelock lock = new reentrantreadwritelock()      (int = 1; <= 10; i++) {         decrementer d = new decrementer(counter, lock)         new thread(d).start()     } }  public class decrementer implements runnable {      public final static int max_size = 5     private readwritelock lock     private atomicinteger counter      public decrementer(atomicinteger counter, readwritelock lock) {         this.counter = counter         this.lock = lock     }      public void run() {         while (true) {             try{                 lock.readlock().lock()                 int current = this.counter.decrementandget()                 system.out.println(thread.currentthread().getname() + " @ counter " + current)                 thread.sleep(762)             } {                 lock.readlock().unlock()             }              try {                 lock.writelock().lock()                 int current = this.counter.get()                 if (current <= 0) {                     this.counter.set(decrementer.max_size)                     system.out.println(thread.currentthread().getname() + " reset " + current + " " + decrementer.max_size)                     thread.sleep(4217)                 }             } {                 lock.writelock().unlock()             }         }     } } 

which give following weird output (with negative counter value), due missing "synchronized" check of atomicinteger value.

thread-3 @ counter 2 thread-2 @ counter 4 thread-1 @ counter 3 thread-4 @ counter 1 thread-5 @ counter 0 thread-6 @ counter -1 thread-7 @ counter -2 thread-8 @ counter -3 thread-9 @ counter -4 thread-10 @ counter -5 thread-2 reset -5 5 thread-3 @ counter 4 thread-4 @ counter 2 thread-2 @ counter 3 thread-1 @ counter 1 thread-5 @ counter -3 thread-10 @ counter -4 thread-7 @ counter -1 thread-6 @ counter -2 thread-8 @ counter 0 thread-9 @ counter -5 thread-9 reset -5 5 

i saw countdownlatch , cyclicbarrier classes goal not sync threads ensure counter reset atomic , excluding other modification other threads.

do see obvious concurrency issue missed in code ?

your lock vise versa. you're acquiring read lock, doing "write". should work:

public static void main(string[] args) {     atomicinteger counter = new atomicinteger(decrementer.max_size);     readwritelock lock = new reentrantreadwritelock();      (int = 1; <= 10; i++) {         decrementer d = new decrementer(counter, lock);         new thread(d).start();     } }  static public class decrementer implements runnable {      public final static int max_size = 5;     private readwritelock lock;     private atomicinteger counter;      public decrementer(atomicinteger counter, readwritelock lock) {         this.counter = counter;         this.lock = lock;     }      public void run() {         while (true) {             try {                 lock.writelock().lock();                 int current = this.counter.decrementandget();                 system.out.println(thread.currentthread().getname() + " @ counter " + current);                 if (current <= 0) {                     this.counter.set(decrementer.max_size);                     system.out.println(thread.currentthread().getname() + " reset " + current + " " + decrementer.max_size);                     thread.sleep(4217);                 }             } catch (interruptedexception e) {                 e.printstacktrace();             } {                 lock.writelock().unlock();             }         }     } } 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -