Hallo,

all literature about concurrent proagramming says: never use wait
without a loop. Another Thread can call Thread.interrupt(), so it can
happen you that you are trying to dequeue a message from an empty
queue. The code should go like this (without the need for
synchronized en/dequeue methods):

/** Thread that sends all message to the clients */
public void run() {
    Message message;

    synchronized(queue){
        while (queue.size() == 0) {
            try {
                // gives up the lock,
                // reaquires it on return
                queue.wait();
            } catch(InterruptedException ie) {
                // possibly terminate
                if (globalTeminateFlag)
                    return;

                // else see if we got a message
            }
       }

       message = queue.dequeue();
   }
   proccessMessage(message);
}

public void sendToClients(Message message) {
    synchronized(queue){
        queue.enqueue(message);
        queue.notifyAll();
    }
}


Cheers, Ole.

Hubinette Per <[EMAIL PROTECTED]> writes:

> This is the  way to go I think but you need to wait and notify on the queue as well 
>as have synchronized blocks.
> Be sure that your queue is thread safe (Vector or some synchronized Set,Map). Don't 
>use thread priority this is not well specified for the JVM. You don't have any 
>control over how the thread priority will work.
>
> /** Thread that sends all message to the clients */
> public void run() {
>
>   if(queue.size() == 0) {
>     synchronized(queue){
>         try{
>           queue.wait();
>         }
>         catch(InterruptedException ie){}
>     }
>    }
>    message = queue.dequeue();
>    proccessMessage(message);
> }
>
> public void sendToClients(Message message) {
>   queue.enqueue(message);
>   synchronized(queue){
>         queue.notifyAll();
>   }
> }
>
> -Per

Cheers

--
Predestination was doomed from the start.

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA3D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to