-
-
Save BillBarnhill/4be4128d5eb397ba11ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private AtomicReferenceArray<Thread> consumerWorker; | |
@Override | |
public void block() throws InterruptedException { | |
boolean parked=false; | |
for(int index=0;index<consumer;index++) | |
{ | |
if(consumerWorker.get(index)==null && consumerWorker.compareAndSet(index, null, Thread.currentThread())) | |
{ | |
parked=true; | |
LockSupport.park(this); | |
break; | |
} | |
} | |
if(!parked) | |
{ | |
//Unable to park because no slots available then park for some time | |
LockSupport.parkNanos(this, TimeUnit.MILLISECONDS.toNanos(10)); | |
} | |
} | |
@Override | |
public void release() { | |
for(int index=0;index<consumer;index++) | |
{ | |
Thread threadToNotify = consumerWorker.get(index); | |
if(threadToNotify!=null) | |
{ | |
consumerWorker.set(index, null); | |
LockSupport.unpark(threadToNotify); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment