Skip to content

Commit

Permalink
Merge pull request apache#6267 from pepness/deque-req-processor
Browse files Browse the repository at this point in the history
- Use Deque/ArrayDeque instead of Stack, the Deque interface should be used in preference to the legacy Stack class, it will give better performance and it is already used in synchronized blocks so there is no need for the thread-safe Stack/Vector class.
- Increase default RequestProcessor throughput from 50 to 55.
  • Loading branch information
pepness authored Aug 6, 2023
2 parents 25efb2e + cc63dbd commit 7d16bfe
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions platform/openide.util/src/org/openide/util/RequestProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@

import java.lang.reflect.Method;
import java.security.PrivilegedAction;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Set;
import java.util.SortedSet;
import java.util.Stack;
import java.util.TreeSet;
import java.util.WeakHashMap;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -183,8 +184,8 @@ public final class RequestProcessor implements ScheduledExecutorService {
boolean slow = false;
assert slow = true;
SLOW = slow;
// 50: a conservative value, just for case of misuse
UNLIMITED = new RequestProcessor("Default RequestProcessor", 50, false, SLOW, SLOW ? 3 : 0); // NOI18N
// 55: a conservative value, just for case of misuse
UNLIMITED = new RequestProcessor("Default RequestProcessor", 55, false, SLOW, SLOW ? 3 : 0); // NOI18N
}

/** The name of the RequestProcessor instance */
Expand Down Expand Up @@ -1863,7 +1864,7 @@ public Throwable fillInStackTrace() {
*/
private static class Processor extends Thread {
/** A stack containing all the inactive Processors */
private static final Stack<Processor> pool = new Stack<Processor>();
private static final Deque<Processor> POOL = new ArrayDeque<>();

/* One minute of inactivity and the Thread will die if not assigned */
private static final int INACTIVE_TIMEOUT = Integer.getInteger("org.openide.util.RequestProcessor.inactiveTime", 60000); // NOI18N
Expand All @@ -1886,7 +1887,7 @@ private static class Processor extends Thread {
public Processor() {
super(TOP_GROUP.getTopLevelThreadGroup(), "Inactive RequestProcessor thread"); // NOI18N
setDaemon(true);
assert !Thread.holdsLock(pool); // new Thread may lead to huge classloading
assert !Thread.holdsLock(POOL); // new Thread may lead to huge classloading
}

/** Provide an inactive Processor instance. It will return either
Expand All @@ -1898,8 +1899,8 @@ public Processor() {
static Processor get() {
Processor newP = null;
for (;;) {
synchronized (pool) {
if (pool.isEmpty()) {
synchronized (POOL) {
if (POOL.isEmpty()) {
if (newP != null) {
Processor proc = newP;
proc.idle = false;
Expand All @@ -1909,7 +1910,7 @@ static Processor get() {
}
} else {
assert checkAccess(TOP_GROUP.getTopLevelThreadGroup());
Processor proc = pool.pop();
Processor proc = POOL.pop();
proc.idle = false;

return proc;
Expand All @@ -1929,10 +1930,10 @@ private static boolean checkAccess(ThreadGroup g) throws SecurityException {
* @param last the debugging string identifying the last client.
*/
static void put(Processor proc, String last) {
synchronized (pool) {
synchronized (POOL) {
proc.setName("Inactive RequestProcessor thread [Was:" + proc.getName() + "/" + last + "]"); // NOI18N
proc.idle = true;
pool.push(proc);
POOL.push(proc);
}
}

Expand Down Expand Up @@ -1986,9 +1987,9 @@ public void run() {

if (current == null) { // We've timeouted

synchronized (pool) {
synchronized (POOL) {
if (idle) { // and we're idle
pool.remove(this);
POOL.remove(this);

break; // exit the thread
} else { // this will happen if we've been just
Expand Down

0 comments on commit 7d16bfe

Please sign in to comment.