🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #17387

Re: Java Wait and Notify - Advice?

#17387
From: Dave Howorth
Date: Tue, 17 Nov 1998 00:00
62 lines
2750 bytes
Mark Beecroft wrote ...
>When the programme kicks off, the main thread is started as usual and
>proceeds until it hits a Wait()
>at which point the crypto thread takes over. Having completed the
>cryptographic methods, a Notify() is performed on the main thread which
duly
>reaquires control (since both threads are the same priority, I assume).

Mark, you seem to say that the main thread is always idle while the crypto
thread is busy, and the crypto thread is always idle while the main thread
is busy. If so, why have separate threads? Why not just have a method call
to do the crypto stuff?

>I assume the crypto thread is still available, but is simply not live.

After the crypto thread does the Notify(), *both* threads are runnable, and
if you were on a multiprocessor machine, they would likely run in parallel.

>I have looked at Wait()s and Notify()s on the crypto thread itself, but I
>keep coming accross the situation whereby, if I do a Notify() on the main
>thread before the declaration of the Wait() on the crypto thread, the
latter
>will never occur.

Three suggestions:
(1) Remember that wait() and notify() are methods of class Object. You can
use them on anything at all. Usually it will be better to wait on something
smaller than a Thread; perhaps an inter-thread message.
(2) Put all your synchronization code (for each pattern of inter-thread
activity) inside one class, and think of it in terms of the class's
behaviour rather than the threads. You may be doing this already, it wasn't
clear.
(3) If you come across a situation where there's a possibility the notify()
might occur before the wait() you can do something like this:

class LeftLuggage
{
    Object locker = null;

    public synchronized Object swap(Object parcel)
    {
        Object anotherParcel;

        if (locker == null)
        {
            locker = parcel;
            this.wait();
            anotherParcel = locker;
            locker = null;
        }
        else
        {
            anotherParcel = locker;
            locker = parcel;
            this.notify();
        }

        return anotherParcel;
    }
}

/* Health warning: it's late at night and I haven't run this :-) */
Technical Details
Message-ID: <72qqae$saf8@forums.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <72pk2c$qiu21@forums.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,8889eb48cc4dd825
X-Google-Attributes: gidf74b3,public
From: "Dave Howorth" <5bx@iname.com>
Subject: Re: Java Wait and Notify - Advice?
Date: 1998/11/17
Message-ID: <72qqae$saf8@forums.borland.com>
X-Deja-AN: 412637638
References: <72pk2c$qiu21@forums.borland.com>
X-MimeOLE: Produced By Microsoft MimeOLE V4.71.1712.3
Organization: Another Netscape Collabra Server User
Newsgroups: borland.public.jbuilder.java.language