Results 1 to 7 of 7
Thread: how to "notifyAll()" ?
- 02-04-2011, 08:32 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
how to "notifyAll()" ?
hi ,
i've made something like this :
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 allJava 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
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
- 02-05-2011, 11:27 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-05-2011, 07:13 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
thanks . that really helped but how many monitors are there ? how can i sync an object ?
class2 looks like this :
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)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
Last edited by MTN; 02-05-2011 at 07:16 PM.
- 02-05-2011, 09:59 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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.
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.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)
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-06-2011, 05:30 AM #5
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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...
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'.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();
- 02-06-2011, 02:06 PM #6
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
@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
-
You don't synchronize an object. You synchronize a method or block on an object.
Synchronization tutorial
Similar Threads
-
connection = DriverManager.getConnection(DATABASE_URL,'"+userid +"','"+password+"');
By renu in forum New To JavaReplies: 3Last Post: 10-12-2010, 04:21 PM -
Java, Military Format using "/" and "%" Operator!!
By sk8rsam77 in forum New To JavaReplies: 11Last Post: 02-26-2010, 03:03 AM -
How to change my form design from "metal" to "nimbus" in Netbeans 6.7.1?
By mlibot in forum New To JavaReplies: 1Last Post: 01-21-2010, 09:20 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks