Thread-safe функции RequestWorker

This commit is contained in:
Gravit 2018-11-11 16:16:05 +07:00
parent f21572879e
commit 5c01d5fd17
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
2 changed files with 12 additions and 3 deletions

View file

@ -9,6 +9,7 @@
import ru.gravit.launcher.request.websockets.RequestInterface; import ru.gravit.launcher.request.websockets.RequestInterface;
import ru.gravit.launcher.serialize.signed.SignedObjectHolder; import ru.gravit.launcher.serialize.signed.SignedObjectHolder;
import ru.gravit.utils.helper.CommonHelper; import ru.gravit.utils.helper.CommonHelper;
import ru.gravit.utils.helper.LogHelper;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
@ -58,6 +59,10 @@ public void makeJsonRequest(RequestInterface request, Runnable callback)
@LauncherAPI @LauncherAPI
public void startTask(Task task) public void startTask(Task task)
{ {
worker.queue.offer(task); try {
worker.queue.put(task);
} catch (InterruptedException e) {
LogHelper.error(e);
}
} }
} }

View file

@ -18,9 +18,13 @@ public RequestWorker()
public void run() { public void run() {
while (!Thread.interrupted()) while (!Thread.interrupted())
{ {
Task task = queue.poll(); try {
if (task != null) { Task task;
task = queue.take();
task.run(); task.run();
} catch (InterruptedException e) {
LogHelper.error(e);
return;
} }
} }
} }