All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.koushikdutta.async.ThreadQueue Maven / Gradle / Ivy

Go to download

Asynchronous socket, http(s) (client+server) and websocket library for android. Based on nio, not threads.

There is a newer version: 3.1.0
Show newest version
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();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy