Archiviert
13
0

Add the ability to customize the worker threads.

Dieser Commit ist enthalten in:
Kristian S. Stangeland 2012-10-10 07:16:01 +02:00
Ursprung 17b7526fe9
Commit 4301dc6525
3 geänderte Dateien mit 60 neuen und 5 gelöschten Zeilen

Datei anzeigen

@ -17,6 +17,11 @@
package com.comphenix.protocol; package com.comphenix.protocol;
/**
* Ignore this class.
*
* @author Kristian
*/
public class Application { public class Application {
public static void main(String[] args) { public static void main(String[] args) {
// For now, though we might consider making a proper application // For now, though we might consider making a proper application

Datei anzeigen

@ -30,6 +30,11 @@ import com.comphenix.protocol.injector.PacketFilterManager;
import com.comphenix.protocol.metrics.Statistics; import com.comphenix.protocol.metrics.Statistics;
import com.comphenix.protocol.reflect.compiler.BackgroundCompiler; import com.comphenix.protocol.reflect.compiler.BackgroundCompiler;
/**
* The main entry point for ProtocolLib.
*
* @author Kristian
*/
public class ProtocolLibrary extends JavaPlugin { public class ProtocolLibrary extends JavaPlugin {
// There should only be one protocol manager, so we'll make it static // There should only be one protocol manager, so we'll make it static

Datei anzeigen

@ -13,6 +13,7 @@ import com.comphenix.protocol.events.ListeningWhitelist;
import com.comphenix.protocol.events.PacketAdapter; import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketEvent; import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.events.PacketListener; import com.comphenix.protocol.events.PacketListener;
import com.google.common.base.Function;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
/** /**
@ -200,7 +201,7 @@ public class AsyncListenerHandler {
} }
/** /**
* Start a singler worker thread handling the asynchronous. * Start a singler worker thread handling the asynchronous listener.
*/ */
public synchronized void start() { public synchronized void start() {
if (listener.getPlugin() == null) if (listener.getPlugin() == null)
@ -213,11 +214,55 @@ public class AsyncListenerHandler {
filterManager.scheduleAsyncTask(listener.getPlugin(), new Runnable() { filterManager.scheduleAsyncTask(listener.getPlugin(), new Runnable() {
@Override @Override
public void run() { public void run() {
Thread thread = Thread.currentThread();
String previousName = thread.getName();
String workerName = getFriendlyWorkerName(listenerLoop.getID()); String workerName = getFriendlyWorkerName(listenerLoop.getID());
// Add the friendly worker name // Add the friendly worker name
Thread.currentThread().setName(workerName); thread.setName(workerName);
listenerLoop.run(); listenerLoop.run();
thread.setName(previousName);
}
});
}
/**
* Start a singler worker thread handling the asynchronous listener.
* <p>
* This method is intended to allow callers to customize the thread priority
* before the worker loop is actually called. This is simpler than to
* schedule the worker threads manually.
* <pre><code>
* listenerHandler.start(new Function&lt;AsyncRunnable, Void&gt;() {
* &#64;Override
* public Void apply(&#64;Nullable AsyncRunnable workerLoop) {
* Thread thread = Thread.currentThread();
* int prevPriority = thread.getPriority();
*
* thread.setPriority(Thread.MIN_PRIORITY);
* workerLoop.run();
* thread.setPriority(prevPriority);
* return null;
* }
* });
* }
* </code></pre>
* @param executor - a method that will execute the given listener loop.
*/
public synchronized void start(Function<AsyncRunnable, Void> executor) {
if (listener.getPlugin() == null)
throw new IllegalArgumentException("Cannot start task without a valid plugin.");
if (cancelled)
throw new IllegalStateException("Cannot start a worker when the listener is closing.");
final AsyncRunnable listenerLoop = getListenerLoop();
final Function<AsyncRunnable, Void> delegateCopy = executor;
filterManager.scheduleAsyncTask(listener.getPlugin(), new Runnable() {
@Override
public void run() {
delegateCopy.apply(listenerLoop);
} }
}); });
} }
@ -300,7 +345,7 @@ public class AsyncListenerHandler {
stop(); stop();
// May happen if another thread is doing something similar to "setWorkers" // May happen if another thread is doing something similar to "setWorkers"
if ((System.currentTimeMillis() - time) > 1000) if ((System.currentTimeMillis() - time) > 50)
throw new RuntimeException("Failed to set worker count."); throw new RuntimeException("Failed to set worker count.");
} }
} }