Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RESTEASY-3314] Use a global executor service to avoid creating too m…
…any executors. https://issues.redhat.com/browse/RESTEASY-3314 Signed-off-by: James R. Perkins <jperkins@redhat.com>
- Loading branch information
Showing
6 changed files
with
332 additions
and
53 deletions.
There are no files selected for viewing
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
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
59 changes: 59 additions & 0 deletions
59
resteasy-core-spi/src/main/java/org/jboss/resteasy/concurrent/ContextualThreadFactory.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* | ||
* Copyright 2023 Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.jboss.resteasy.concurrent; | ||
|
||
import java.security.AccessController; | ||
import java.security.PrivilegedAction; | ||
import java.util.concurrent.ThreadFactory; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
/** | ||
* Creates contextual threads that are always daemon threads. | ||
* | ||
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a> | ||
*/ | ||
class ContextualThreadFactory implements ThreadFactory { | ||
private static final AtomicInteger POOL_COUNTER = new AtomicInteger(0); | ||
private final AtomicInteger threadCounter = new AtomicInteger(0); | ||
private final String prefix; | ||
|
||
ContextualThreadFactory(final String prefix) { | ||
this.prefix = String.format("%s-%d-thread", prefix, POOL_COUNTER.incrementAndGet()); | ||
} | ||
|
||
@Override | ||
public Thread newThread(final Runnable r) { | ||
final Thread thread = new Thread(r, String.format("%s-%d", prefix, threadCounter.incrementAndGet())); | ||
if (System.getSecurityManager() == null) { | ||
thread.setDaemon(true); | ||
if (thread.getPriority() != Thread.NORM_PRIORITY) { | ||
thread.setPriority(Thread.NORM_PRIORITY); | ||
} | ||
return thread; | ||
} | ||
return AccessController.doPrivileged((PrivilegedAction<Thread>) () -> { | ||
thread.setDaemon(true); | ||
if (thread.getPriority() != Thread.NORM_PRIORITY) { | ||
thread.setPriority(Thread.NORM_PRIORITY); | ||
} | ||
return thread; | ||
}); | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
...core-spi/src/main/java/org/jboss/resteasy/concurrent/GlobalContextualExecutorService.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* | ||
* Copyright 2023 Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.jboss.resteasy.concurrent; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a> | ||
*/ | ||
class GlobalContextualExecutorService extends ContextualExecutorService implements AutoCloseable { | ||
static final GlobalContextualExecutorService INSTANCE = new GlobalContextualExecutorService(); | ||
private final Thread shutdownHook; | ||
|
||
private volatile ExecutorService delegate; | ||
|
||
private GlobalContextualExecutorService() { | ||
super(null, true); | ||
shutdownHook = new Thread("resteasy-shutdown") { | ||
@Override | ||
public void run() { | ||
synchronized (GlobalContextualExecutorService.this) { | ||
if (delegate != null) { | ||
delegate.shutdown(); | ||
delegate = null; | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public void shutdown() { | ||
// Do nothing as we will shut it down later | ||
} | ||
|
||
@Override | ||
public List<Runnable> shutdownNow() { | ||
return Collections.emptyList(); | ||
} | ||
|
||
@Override | ||
public boolean isShutdown() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean isTerminated() { | ||
return false; | ||
} | ||
|
||
@Override | ||
ExecutorService getDelegate() { | ||
if (delegate == null) { | ||
synchronized (this) { | ||
if (delegate == null) { | ||
final int poolSize = SecurityActions.getCoreThreads("dev.resteasy.concurrent.core.pool.size"); | ||
delegate = new ThreadPoolExecutor(poolSize, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, | ||
new LinkedBlockingQueue<>(), new ContextualThreadFactory("contextual-pool")); | ||
SecurityActions.registerShutdownHook(shutdownHook); | ||
} | ||
} | ||
} | ||
return delegate; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
synchronized (this) { | ||
SecurityActions.removeShutdownHook(shutdownHook); | ||
if (delegate != null) { | ||
delegate.shutdown(); | ||
delegate = null; | ||
} | ||
} | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
...src/main/java/org/jboss/resteasy/concurrent/GlobalContextualScheduledExecutorService.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* | ||
* Copyright 2023 Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.jboss.resteasy.concurrent; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
|
||
/** | ||
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a> | ||
*/ | ||
class GlobalContextualScheduledExecutorService extends ContextualScheduledExecutorService implements AutoCloseable { | ||
static final GlobalContextualScheduledExecutorService INSTANCE = new GlobalContextualScheduledExecutorService(); | ||
private final Thread shutdownHook; | ||
private volatile ScheduledExecutorService delegate; | ||
|
||
private GlobalContextualScheduledExecutorService() { | ||
super(null, true); | ||
shutdownHook = new Thread("resteasy-shutdown") { | ||
@Override | ||
public void run() { | ||
synchronized (GlobalContextualScheduledExecutorService.this) { | ||
if (delegate != null) { | ||
delegate.shutdown(); | ||
delegate = null; | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public void shutdown() { | ||
// Do nothing as we will shut it down later | ||
} | ||
|
||
@Override | ||
public List<Runnable> shutdownNow() { | ||
return Collections.emptyList(); | ||
} | ||
|
||
@Override | ||
public boolean isShutdown() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean isTerminated() { | ||
return false; | ||
} | ||
|
||
@Override | ||
ScheduledExecutorService getDelegate() { | ||
if (delegate == null) { | ||
synchronized (this) { | ||
if (delegate == null) { | ||
final int poolSize = SecurityActions.getCoreThreads("resteasy.async.timeout.scheduler.min.pool.size"); | ||
delegate = Executors.newScheduledThreadPool(poolSize, | ||
new ContextualThreadFactory("contextual-scheduled-pool")); | ||
SecurityActions.registerShutdownHook(shutdownHook); | ||
} | ||
} | ||
} | ||
return delegate; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
synchronized (this) { | ||
SecurityActions.removeShutdownHook(shutdownHook); | ||
if (delegate != null) { | ||
delegate.shutdown(); | ||
delegate = null; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.