/** * Blocks until messages are available * @param num_spins the number of times we should spin before acquiring a lock * @param wait_strategy the strategy used to spin. The first parameter is the iteration count and the second * parameter is the max number of spins */ public int waitForMessages(int num_spins, final BiConsumer<Integer,Integer> wait_strategy) throws InterruptedException { // try spinning first (experimental) for(int i=0; i < num_spins && count == 0; i++) { if(wait_strategy != null) wait_strategy.accept(i, num_spins); else Thread.yield(); } if(count == 0) { lock.lock(); try { while(count == 0) not_empty.await(); } finally { lock.unlock(); } } return count; // whatever is the last count; could have been updated since lock release }
/** * Blocks until messages are available * @param num_spins the number of times we should spin before acquiring a lock * @param wait_strategy the strategy used to spin. The first parameter is the iteration count and the second * parameter is the max number of spins */ public int waitForMessages(int num_spins, final BiConsumer<Integer,Integer> wait_strategy) throws InterruptedException { // try spinning first (experimental) for(int i=0; i < num_spins && count == 0; i++) { if(wait_strategy != null) wait_strategy.accept(i, num_spins); else Thread.yield(); } if(count == 0) { lock.lock(); try { while(count == 0) not_empty.await(); } finally { lock.unlock(); } } return count; // whatever is the last count; could have been updated since lock release }
/** * Blocks until messages are available * @param num_spins the number of times we should spin before acquiring a lock * @param wait_strategy the strategy used to spin. The first parameter is the iteration count and the second * parameter is the max number of spins */ public int waitForMessages(int num_spins, final BiConsumer<Integer,Integer> wait_strategy) throws InterruptedException { // try spinning first (experimental) for(int i=0; i < num_spins && count == 0; i++) { if(wait_strategy != null) wait_strategy.accept(i, num_spins); else Thread.yield(); } if(count == 0) { lock.lock(); try { while(count == 0) not_empty.await(); } finally { lock.unlock(); } } return count; // whatever is the last count; could have been updated since lock release }