[Java lista] szálkezelés

Peter Verhas peter at verhas.com
2009. Május. 29., P, 12:58:37 CEST


Én a mellékelt kódot raktam egyszer össze (nem teljesen saját
kútfőből). Lehet használni, mint szegény ember JMS-e, de mivel nagyon
rövid és tömör a kód, tanulni sem rossz amíg megérti az ember, hogy mi
hogyan működik benne.

Aki pedig használni akarja, és kell a sokkal összetettebb és több
funkciót nyújtó kód, az a jPOS szoftverben keresheti.

-- 
Verhás Péter
ügyvezető
Verhás & Verhás Szoftver Manufaktúra Kft.
peter at verhas.com
t: +36(30)9306805


package com.verhas.util;

import java.util.LinkedList;

/**
 * A simple memory queue implementation that may help synchronization between
 * threads. The implementation of this class was inspired by the jPOS Space
 * implementation. This class is much simpler, implementing only one queue
 * that can be use to synchronize instead of a Hash of queues.
 * <p>
 * This implementation is very simple. It does not contains any persistence.
 * This is mainly to synchronize between threads. For any more complex
 * applications utilize JMS.
 * <p>
 * Known issue: when you call {@code receive(timeout)} the code may not return
 * after {@code timeout} millis. This may happen if another thread waits for the
 * top element of the queue with a longer timeout.
 * <p>
 * You can avoid this issue if all threads receiving from the queue use the same
 * timeout value.
 * <p>
 * @author Peter Verhas <peter at verhas.com>
 */
public class MemoryQueue {

    protected LinkedList list = new LinkedList();

    public MemoryQueue() {
    }

    /**
     * Send an object into the queue.
     *
     * @param value
     */
    public synchronized void send(Object value) {
        if (value == null) {
            throw new NullPointerException("You can not send null into
a queue.");
        }
        synchronized (this) {
            list.add(value);
            this.notifyAll();
        }
    }

    /**
     * Gets out the top element of the queue. This is an asynchronous version
     * that returns {@code null} if the queue is empty.
     * <p>
     *
     * @return an object from the queue or {@code null}.
     */
    private synchronized Object receiveAsync() {
        Object obj = list.isEmpty() ? null : list.get(0);
        if (obj != null) {
            list.remove(0);
        }
        return obj;
    }

    /**
     * Get the top element of the queue. Wait infinitely until there is an
     * element available.
     *
     * @return an object from the queue.
     */
    public synchronized Object receive() {
        Object obj;
        while ((obj = receiveAsync()) == null) {
            try {
                this.wait();
            } catch (InterruptedException e) {
            }
        }
        return obj;
    }

    private synchronized Object receiveTill(long end) {
        Object obj = null;
        long now;
        while ((obj = receiveAsync()) == null &&
                ((now = System.currentTimeMillis()) < end)) {
            try {
                this.wait(end - now);
            } catch (InterruptedException e) {
            }
        }
        return obj;
    }

    /**
     * Get the top element of the queue or return {@code null} if there is none
     * within {@code timeout} millis.
     *
     * @param timeout
     * @return the object or null
     */
    public Object receive(long timeout) {
        long now = System.currentTimeMillis();
        long end = now + timeout;
        return receiveTill(end);
    }
}


További információk a(z) Javalist levelezőlistáról