Results 1 to 7 of 7
  1. #1
    MTN
    MTN is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default how to "notifyAll()" ?

    hi ,
    i've made something like this :
    Java Code:
    class 1 extends Thread:
    do some stuff ...
    public void run :look for a client and create an instance of class2 for it if found one
    
    class2 extends Thread:
    public void run : do some stuff and notifyAll();
    
    inner class extends thread : 
    public void run : 
    while(class2's thread isn't done yet) : wait();
    do some stuff and notifyAll() again
    so here's my problem : i want it to notify all of the threads from all of the objects made of class2 . i thought that was the difference between notify() and notifyAll() but apparently it doesn't notify all
    i think it has something to do with the monitor (whatever that is) . how can i pass it along all objects and notify their threads ?

    p.s. ^ is a messenger . class1 looks for sockets , class2 accepts them and sets up the stream to communicate with the client. class2's thread reads whatever i send from the client and the inner class sends every one's messages back

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,348
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by MTN View Post
    i think it has something to do with the monitor (whatever that is)
    Knowing what a monitor is might help understanding what synchronization, wait and notify do: a monitor is like a hotel room with an adjacent bath room; only one person can enter the hotel room (synchronize) and that person can enter the bath room (wait) so the hotel room becomes free again for another person (thread) to enter it. Persons (threads) waiting in the bath room can only leave that room when another (single) person in the hotel room takes them (or only one of them) out of it (notify). A hotel room and its adjacent bath room can be associated to any single object. If nobody is in the bath room and nobody is in the hotel room the monitor is released from its association with the object.

    This is what synchronization does: it creates a hotel room (if not already done so) and associates it with the object (the target of the synchronization). If there already was such an association it is checked if some other thread is in the hotel room. If so, the current thread is blocked. If not (other threads are in the bath room because the association already exists), the current thread can enter the hotel room.

    Waiting simply is leaving the hotel room and entering the bath room (which is possibly (not) empty) and stay there.

    Notifying can only be done from the hotel room (so a synchronization had succeeded previously) and opening the bath room door and release one or all of the waiting threads. The bath room door only opens when the notifying thread has left the room. Only one of those woken up threads can enter the hotel room, the others have to wait in the bath room again.

    So if you want to notify all threads in a certain bath room, you have to synchronize on that particular hotel room and open the door to the adjacent bath room (and leave the hotel room again).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    MTN
    MTN is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    thanks . that really helped but how many monitors are there ? how can i sync an object ?
    class2 looks like this :
    Java Code:
    class2' public void run : read();
    inner class' public void run : put();
    
    synchronized void read() : write messages to vector; notifyAll();
    synchronized void put() : while(no new messages) wait(); //waits until it is notified by read() 
    after a new message is recieved sends all of the new messages back to the client
    the problem is it won't send new messages to client A until client A itself writes a new message to server (that notifies put)
    Last edited by MTN; 02-05-2011 at 07:16 PM.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,348
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by MTN View Post
    thanks . that really helped but how many monitors are there ? how can i sync an object ?
    There are as many as you need: when you synchronize on an object another monitor/hotel room is associated with that object if not already done so.

    the problem is it won't send new messages to client A until client A itself writes a new message to server (that notifies put)
    So there's something wrong with your locking/synchronizing scheme; I haven't closely looked at it, but that's my guess. Keep that hotel room analogy in mind and design your threads around it; doing it the other way around and hoping that it'll work is (almost) never going to work.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Quote Originally Posted by MTN View Post
    so here's my problem : i want it to notify all of the threads from all of the objects made of class2 .
    notifyAll notifies all threads waiting on the monitor of a specific object (instance of a class). It does not broadcast to all monitors of all instances of a given class.

    For example...

    Java Code:
    Object a = new Object();
    Object b = new Object();
    
    ...some thread
    b.wait();
    
    ....some other thread
    a.wait();
    ...yet another thread
    a.wait();
    ....and some other thread still
    a.notifyAll();
    The notifyAll on 'a' in this pseudo-code is not going to notify the thread waiting on 'b', but it will notify all the threads waiting on 'a'.

  6. #6
    MTN
    MTN is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    @JosAH : i know what you mean but how do i synchronize an object ? just calling a synchronized function of that object assigns a new monitor to it(if not already ...) ?
    also about the bathroom , the way i see it theres only one way it'd work and that is if theres only one bathroom but many hotel rooms so every object can notify other class' threads , otherwise i just have to create a thread in the client to ask for validation every 1 seconds (overwork the server)
    @toadaly : yes i see that ... now :D

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Replies: 3
    Last Post: 10-12-2010, 04:21 PM
  2. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 03:03 AM
  3. Replies: 1
    Last Post: 01-21-2010, 09:20 AM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •