Skip to content

FELIX-6784 Reduce number of thread (re)creations of the Configurator Worker Thread #427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public Bundle addingBundle(final Bundle bundle, final BundleEvent event) {
if ( active &&
(state == Bundle.ACTIVE || state == Bundle.STARTING) ) {
SystemLogger.debug("Adding bundle " + getBundleIdentity(bundle) + " : " + getBundleState(state));
queue.enqueue(new Runnable() {
queue.submit(new Runnable() {

@Override
public void run() {
Expand All @@ -128,7 +128,7 @@ public void removedBundle(final Bundle bundle, final BundleEvent event, final Bu
final int state = bundle.getState();
if ( active && state == Bundle.UNINSTALLED ) {
SystemLogger.debug("Removing bundle " + getBundleIdentity(bundle) + " : " + getBundleState(state));
queue.enqueue(new Runnable() {
queue.submit(new Runnable() {

@Override
public void run() {
Expand All @@ -148,7 +148,7 @@ public void run() {
}

public void configAdminAdded() {
queue.enqueue(new Runnable() {
queue.submit(new Runnable() {

@Override
public void run() {
Expand Down Expand Up @@ -182,7 +182,7 @@ private String getBundleState(int state) {
*/
public void shutdown() {
this.active = false;
this.queue.stop();
this.queue.shutdownNow();
this.tracker.close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,67 +18,32 @@
*/
package org.apache.felix.configurator.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

import org.apache.felix.configurator.impl.logger.SystemLogger;

public class WorkerQueue implements Runnable {

private final ThreadFactory threadFactory;

private final List<Runnable> tasks = new ArrayList<>();

private volatile Thread backgroundThread;

private volatile boolean stopped = false;
public class WorkerQueue extends ScheduledThreadPoolExecutor {

public WorkerQueue() {
this.threadFactory = Executors.defaultThreadFactory();
}

public void stop() {
synchronized ( this.tasks ) {
this.stopped = true;
}
}

public void enqueue(final Runnable r) {
synchronized ( this.tasks ) {
if ( !this.stopped ) {
this.tasks.add(r);
if ( this.backgroundThread == null ) {
this.backgroundThread = this.threadFactory.newThread(this);
this.backgroundThread.setDaemon(true);
this.backgroundThread.setName("Apache Felix Configurator Worker Thread");
this.backgroundThread.start();
}
super(1, new ThreadFactory() {
@Override
public Thread newThread(Runnable runnable) {
Thread thread = new Thread(runnable);
thread.setDaemon(true);
thread.setName("Apache Felix Configurator Worker Thread");
return thread;
}
}
});
setKeepAliveTime(5, TimeUnit.SECONDS);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be made configurable, but my initial proposal is to have a static, relatively short, keep alive time fixed in the code. Its purpose is mainly to let the underlying thread terminate at some point if no tasks are queued.

allowCoreThreadTimeOut(true);
}

@Override
public void run() {
Runnable r;
do {
r = null;
synchronized ( this.tasks ) {
if ( !this.stopped && !this.tasks.isEmpty() ) {
r = this.tasks.remove(0);
} else {
this.backgroundThread = null;
}
}
if ( r != null ) {
try {
r.run();
} catch ( final Throwable t) {
// just to be sure our loop never dies
SystemLogger.error("Error processing task" + t.getMessage(), t);
}
}
} while ( r != null );
protected void afterExecute(Runnable runnable, Throwable throwable) {
if (throwable != null) {
SystemLogger.error("Error processing task" + throwable.getMessage(), throwable);
}
}
}