我正在尝试在OSGi中实现一个服务,它应该等待来自另一个包的传入数据并在接收到数据时处理数据。我正在使用 a LinkedBlockingQueue
,因为我不知道我会收到多少个数据包。我的代码如下所示:
public class MyClass {
protected static LinkedBlockingQueue<JSONObject> inputQueue = new LinkedBlockingQueue<JSONObject>();
private ExecutorService workerpool = Executors.newFixedThreadPool(4);
public void startMyBundle() {
start();
}
protected void start() {
new Thread(new Runnable() {
public void run() {
while(true){
workerpool.execute(new Runnable() {
public void run() {
try {
process(inputQueue.take());
} catch (InterruptedException e) {
System.out.println("thread was interrupted.");
}
}
});
}
}
}).start();
}
public void transmitIn(JSONObject packet) {
try {
inputQueue.put(packet);
} catch (InterruptedException e) {
}
}
protected void process(JSONObject packet) {
//Some processing
}
当我运行它并且只向服务发送一个数据包时,首先会按原样处理数据包,但随后我的处理器会使用其所有容量,并且大多数时候我OutOfMemoryError
看起来像这样:
java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "[Timer] - Periodical Task (Bundle 46) (Bundle 46)"
这可能是什么原因?