async
This commit is contained in:
parent
7a5b2d72ca
commit
39d99685d1
|
@ -9,11 +9,20 @@ import org.json.JSONObject;
|
||||||
public class AsyncHandler implements Runnable {
|
public class AsyncHandler implements Runnable {
|
||||||
|
|
||||||
private List<Task> tasks = new LinkedList<>();
|
private List<Task> tasks = new LinkedList<>();
|
||||||
private Thread asynchandlerthread = null;
|
private Thread[] asynchandlerthread;
|
||||||
|
private int threadsRunning = 0;
|
||||||
private TelegramAPI api;
|
private TelegramAPI api;
|
||||||
|
private static final String THREADPREFIX = "AsyncTgHandler-";
|
||||||
|
|
||||||
|
//just use 1 async thread
|
||||||
public AsyncHandler(TelegramAPI api) {
|
public AsyncHandler(TelegramAPI api) {
|
||||||
|
this(api, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
//allow as many as threadCount threads to handle Async Tasks
|
||||||
|
public AsyncHandler(TelegramAPI api, int threadCount) {
|
||||||
this.api = api;
|
this.api = api;
|
||||||
|
asynchandlerthread = new Thread[threadCount];
|
||||||
}
|
}
|
||||||
|
|
||||||
public void enque(Task t) {
|
public void enque(Task t) {
|
||||||
|
@ -31,19 +40,34 @@ public class AsyncHandler implements Runnable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//make sure its running
|
//muss nicht im syncronized liegen, weil es nur ein read ist
|
||||||
if(asynchandlerthread == null) {
|
ensureThreads(tasks.size());
|
||||||
asynchandlerthread = new Thread(this, "AsyncHandler");
|
|
||||||
asynchandlerthread.start();
|
|
||||||
} else if(!asynchandlerthread.isAlive()) {
|
|
||||||
asynchandlerthread = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void enque(String request, String parameters) {
|
public void enque(String request, String parameters) {
|
||||||
enque(new Task(request, parameters));
|
enque(new Task(request, parameters));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//makes sure, that at least count Threads are running
|
||||||
|
private void ensureThreads(int count) {
|
||||||
|
if(count < 1) count = 1;
|
||||||
|
|
||||||
|
synchronized (asynchandlerthread) {
|
||||||
|
if(threadsRunning >= count) return; //mus im syncronized liegen, damit es nicht zu problemen kommt, wenn diese funktion parallel aufgerufen wird
|
||||||
|
|
||||||
|
//alle threads durchgehen und leere (null) mit neuen Threads füllen
|
||||||
|
for(int i = 0; i < asynchandlerthread.length && count > 0; i++) {
|
||||||
|
if(asynchandlerthread[i] == null) {
|
||||||
|
//spawn a thread
|
||||||
|
asynchandlerthread[i] = new Thread(this, THREADPREFIX + i);
|
||||||
|
asynchandlerthread[i].start();
|
||||||
|
threadsRunning++;
|
||||||
|
}
|
||||||
|
count --;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
int failed = 0;
|
int failed = 0;
|
||||||
|
@ -68,7 +92,7 @@ public class AsyncHandler implements Runnable {
|
||||||
// throw new Exception("Callbacktype missmatch! Got " + obj.getClass().getSimpleName() + " Wanted: " + wanted.getSimpleName() );
|
// throw new Exception("Callbacktype missmatch! Got " + obj.getClass().getSimpleName() + " Wanted: " + wanted.getSimpleName() );
|
||||||
}
|
}
|
||||||
failed = 0;
|
failed = 0;
|
||||||
} catch(UnknownHostException ex) {//host(api.telegram.org) is good -> bad inet
|
} catch(UnknownHostException ex) { //host(api.telegram.org) is good -> bad inet
|
||||||
failed ++;
|
failed ++;
|
||||||
if(failed > 10)
|
if(failed > 10)
|
||||||
try {
|
try {
|
||||||
|
@ -87,7 +111,13 @@ public class AsyncHandler implements Runnable {
|
||||||
t.printStackTrace();
|
t.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
asynchandlerthread = null;
|
|
||||||
|
int threadid = Integer.parseInt(Thread.currentThread().getName().substring(THREADPREFIX.length()));
|
||||||
|
|
||||||
|
synchronized (asynchandlerthread) {
|
||||||
|
threadsRunning --;
|
||||||
|
asynchandlerthread[threadid] = null; //delete thread
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Task {
|
public static class Task {
|
||||||
|
|
Loading…
Reference in New Issue