ARTICLE AD BOX
I have the below code (whole code is here) that polls a queue for runnables and parks itself if the queue is empty.
public void waitAndDrain() throws InterruptedException { throwIfInterrupted(); Runnable runnable = poll(); if (runnable == null) { waiter = Thread.currentThread(); try { while ((runnable = poll()) == null) { LockSupport.park(this); throwIfInterrupted(); } } finally { waiter = null; } } do { runQuietly(runnable); } while ((runnable = poll()) != null); }And the code that does unpark
public void execute(Runnable runnable) { add(runnable); Object waiter = this.waiter; if (waiter != SHUTDOWN) { LockSupport.unpark((Thread) waiter); // no-op if null } else if (remove(runnable) && rejectRunnableOnExecutor) { throw new RejectedExecutionException(); } }If unparks happen in succession, the lost permits are not a problem because the code that unparks also enqueues runnables, and the reader code only parks itself if the queue is not empty. Before you point me to the "else if" block in the unpacking code that looks suspicious, let me point out that this is not an issue, for the "waitor" variable is only ever set to SHUTDOWN here (could have been a private method, need not have been public) after the loop that polls the queue terminates, so if it set the waitor to SHUTDOWN, there is no chance of parking again. However our customer has reported that occasionally the application gets stuck with the parking thread waiting in the parked state:
"Test worker" #1 prio=5 os_prio=0 cpu=68854.65ms elapsed=51528.48s tid=0x0000ffffb40311a0 nid=0x321d1c waiting on condition [0x0000ffffb99fb000] java.lang.Thread.State: WAITING (parking) at jdk.internal.misc.Unsafe.park([email protected]/Native Method) - parking to wait for <0x00000000d69d5a18> (a io.xxxx.stub.ClientCalls$ThreadlessExecutor) at java.util.concurrent.locks.LockSupport.park(LockSupport.java:211) at io.xxxx.stub.ClientCalls$ThreadlessExecutor.waitAndDrain(ClientCalls.java:817) at io.xxxx.stub.ClientCalls.blockingUnaryCall(ClientCalls.java:166) ...I have been splitting hairs unable to come up with a scenario that could cause this. Any idea is much appreciated.
