com.koushikdutta.async.ThreadQueue Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of androidasync Show documentation
Show all versions of androidasync Show documentation
Asynchronous socket, http(s) (client+server) and websocket library for android. Based on nio, not threads.
package com.koushikdutta.async;
import java.util.LinkedList;
import java.util.WeakHashMap;
import java.util.concurrent.Semaphore;
public class ThreadQueue extends LinkedList {
final private static WeakHashMap mThreadQueues = new WeakHashMap();
static ThreadQueue getOrCreateThreadQueue(Thread thread) {
ThreadQueue queue;
synchronized (mThreadQueues) {
queue = mThreadQueues.get(thread);
if (queue == null) {
queue = new ThreadQueue();
mThreadQueues.put(thread, queue);
}
}
return queue;
}
static void release(AsyncSemaphore semaphore) {
synchronized (mThreadQueues) {
for (ThreadQueue threadQueue: mThreadQueues.values()) {
if (threadQueue.waiter == semaphore)
threadQueue.queueSemaphore.release();
}
}
}
AsyncSemaphore waiter;
Semaphore queueSemaphore = new Semaphore(0);
@Override
public boolean add(Runnable object) {
synchronized (this) {
return super.add(object);
}
}
@Override
public boolean remove(Object object) {
synchronized (this) {
return super.remove(object);
}
}
@Override
public Runnable remove() {
synchronized (this) {
if (this.isEmpty())
return null;
return super.remove();
}
}
}